nloah
nloah

Reputation: 11

JTextField NullPointerException when registered in a separate function

I've tried to create a function that will allow me to create a set of JPanel objects in a single swoop. However, when I try to process the data created by the user's input to the field, I get a NullPointerException. If I create the label/field and add them to the panel in my buildPanel method, everything is fine. With this newSet method, I get the error. The objects appear properly in the window, but I can't seem to get the information out of it with my listener. I also tried not passing panel to the function but it makes no difference. Can I register the text field this way at all?

I assume it doesn't apply the textField to the daysF object, and instead is creating a label called "text" that doesn't really exist after the function is done.

private void newSet(int textLength, String labelText, 
                   JTextField text, JLabel label, JPanel panel){
    label = new JLabel(labelText);
    text = new JTextField(textLength);
    panel.add(label);
    panel.add(text);
}

private void buildPanel(){
    panel = new JPanel();       
    newSet(3, "Days gone: ", daysF, daysL, panel);
}   

Here's the listener which converts the text input into a double for some calculations later.

private class CalcButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        double days = fieldToDouble(daysF);
}

I'm getting NullPointerException here, with field.getText().

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

private double fieldToDouble(JTextField field){
    String str = field.getText();
    if (str.equals("")) str = "0";
    double amt = Double.parseDouble(str);
    return amt;
}

Here's the stacktrace as well.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at TravelExpenses.fieldToDouble(TravelExpenses.java:109)
at TravelExpenses.access$1(TravelExpenses.java:108)
at TravelExpenses$CalcButtonListener.actionPerformed(TravelExpenses.java:120)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Upvotes: 1

Views: 92

Answers (0)

Related Questions