Hello,
I'm building a program that gets the value of a TextField, concatenate with some other Strings and then display them on a TextBox when the ScreenCommand is clicked, the problem is that it's calling a NullPointerException. My code is like this(of course it has a lot more stuff):

/* .... */
        } else if (command == submitCommand) {
            FirstPart();
            // write pre-action user code here
            switchDisplayable(null, getTextBox3());
            // write post-action user code here
        }
/* .... */

public void FirstPart() {
    String test = null;
    test = "tst" + textField.getString() + "test";
    textBox3.setString(test);
}
/* .... */

And I get this when I click the menu:

TRACE: <at java.lang.NullPointerException:   0>, Exception caught in Display class
java.lang.NullPointerException:   0
        at mp.releaser.MPReleaser.FirstPart(MPReleaser.java:535)
        at mp.releaser.MPReleaser.commandAction(MPReleaser.java:128)
        at javax.microedition.lcdui.Display$ChameleonTunnel.callScreenListener(), bci=46
        at com.sun.midp.chameleon.layers.SoftButtonLayer.processCommand(), bci=74
        at com.sun.midp.chameleon.layers.SoftButtonLayer.commandSelected(), bci=11
        at com.sun.midp.chameleon.layers.MenuLayer.pointerInput(), bci=188
        at com.sun.midp.chameleon.CWindow.pointerInput(), bci=88
        at javax.microedition.lcdui.Display$DisplayEventConsumerImpl.handlePointerEvent(), bci=19
        at com.sun.midp.lcdui.DisplayEventListener.process(), bci=296
        at com.sun.midp.events.EventQueue.run(), bci=179
        at java.lang.Thread.run(Thread.java:680)

What should I do to correct this?

Best Regards,
Nathan Paulino Campos

*PS: Netbeans should have a `NullPointerException` corrector tool :P*

String test = null? If you want to create an empty string use String test = ""; Not sure if that's where your problem lies.

textField.getString()

Is this a JTextField?

String test = null? If you want to create an empty string use String test = ""; Not sure if that's where your problem lies.

textField.getString()

Is this a JTextField?

This is what I got now:

TRACE: <at java.lang.NullPointerException:   0>, Exception caught in Display class
java.lang.NullPointerException:   0
        at mp.releaser.MPReleaser.commandAction(MPReleaser.java:130)
        at javax.microedition.lcdui.Display$ChameleonTunnel.callScreenListener(), bci=46
        at com.sun.midp.chameleon.layers.SoftButtonLayer.processCommand(), bci=74
        at com.sun.midp.chameleon.layers.SoftButtonLayer.commandSelected(), bci=11
        at com.sun.midp.chameleon.layers.MenuLayer.pointerInput(), bci=188
        at com.sun.midp.chameleon.CWindow.pointerInput(), bci=88
        at javax.microedition.lcdui.Display$DisplayEventConsumerImpl.handlePointerEvent(), bci=19
        at com.sun.midp.lcdui.DisplayEventListener.process(), bci=296
        at com.sun.midp.events.EventQueue.run(), bci=179
        at java.lang.Thread.run(Thread.java:680)

Line 130 Is the last one:

String test = null;
        test = "tst" + textField.getString() + "test";
        textBox3.setString(test);

Yes, I was asking if the textField is JTextField, because there is no getString() function for a JTextField. Where you call textField.getString(), are you sure there is a value received from this? Put a System.out.println(textField.getString()) before you initialise test.

Yes, I was asking if the textField is JTextField, because there is no getString() function for a JTextField. Where you call textField.getString(), are you sure there is a value received from this? Put a System.out.println(textField.getString()) before you initialise test.

textField has text entered :)

Ok, You may be entering text, but this line:

test = "tst" + textField.getString() + "test";

is not storing it. There is no getString() method in either the JTextField class or the TextField class. Therefore, please answer my question as to what class you are using for your textfields.

textField is the name of the class, and the type is a TextField , remember that I'm at J2ME

Ok, well you didn't once say you are using Java ME, even though now looking at your whole error I can see it. But why would I study your errors past the significant place? :). Therefore, I have never used JavaME and cannot help you.

I'm still not seeing a "getString()" method for TextField, or setString(), either. getText() and setText(), yes, but not getString or setString.
This shouldn't be your problem - calling non-existent methods doesn't throw an NPE.
To get this exception, your textBox3 must be null.
Put in this line before the call to textBox3 to test this:

System.out.println("textBox3 "+textBox3==null?"is":"is not"+"null");

I'm still not seeing a "getString()" method for TextField, or setString(), either. getText() and setText(), yes, but not getString or setString.
This shouldn't be your problem - calling non-existent methods doesn't throw an NPE.
To get this exception, your textBox3 must be null.
Put in this line before the call to textBox3 to test this:

System.out.println("textBox3 "+textBox3==null?"is":"is not"+"null");

Now I got this:

is notnull
TRACE: <at java.lang.OutOfMemoryError>, Exception caught in Display class
java.lang.OutOfMemoryError
   (stack trace incomplete)

I'm stumped. Sorry.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.