-1
    Double double;
    int integer = (int) Math.round(double);
    String string = Integer.toString(integer);

    System.out.println(string);
    findTxtField().setText(string);

I am using the above code as An EXAMPLE- I have coded my mobile application using CodeNameOne. The variables are initialized.

I am receiving an error message when running the mobile application and hitting the button that executed similar code(above), unfortunately, after troubleshooting I figured out it was the outputting to the text-field that was causing the crash.

My Mobile application has 5 UI sections. The section for user "registration" will take the data, calculate it and then output the data using a text-field on another UI section "Results".

Any Idea why I am having this issue?

Error:

java.lang.NullPointerException
at userclasses.StateMachine.onMain_BtnInfoAction(StateMachine.java:171)
at generated.StateMachineBase.handleComponentAction(StateMachineBase.java:2251)
at com.codename1.ui.util.UIBuilder$FormListener.actionPerformed(UIBuilder.java:2831)
at com.codename1.ui.util.EventDispatcher.fireActionEvent(EventDispatcher.java:345)
at com.codename1.ui.Button.fireActionEvent(Button.java:397)
at com.codename1.ui.Button.released(Button.java:428)
at com.codename1.ui.Button.pointerReleased(Button.java:516)
at com.codename1.ui.Form.pointerReleased(Form.java:2560)
at com.codename1.ui.Form.pointerReleased(Form.java:2496)
at com.codename1.ui.Component.pointerReleased(Component.java:3108)
at com.codename1.ui.Display.handleEvent(Display.java:2017)
at com.codename1.ui.Display.edtLoopImpl(Display.java:1065)
at com.codename1.ui.Display.mainEDTLoop(Display.java:994)
at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
JSint
  • 101
  • 1
  • 11
  • You don't have a value of `double` variable and its of type `Double` which is a wrapper class, not a primitive. So it is initialized to null. – SacJn Apr 15 '16 at 06:56
  • The double is initialized and used SystemOut to test value. Int was the correct rounded value of the Double. I just can't get it to output in the textfield. I tested the string and it was also the correct value. – JSint Apr 15 '16 at 07:09
  • Issue Resolved. I rebuilt the application and it now functions as intended. Seems as though there must be a compatibility issue. I used 8.1 to build it on a laptop and opened it in a 8.0. Netbeans can be a pain sometimes. This is obviously just a guess though. – JSint Apr 15 '16 at 17:03

3 Answers3

1

You have to find out if the function call findTxtField() returns null. I think it does. If that's the case, try to figure out why. It may be because txtField isn't on the same form as the button. If so, you have to use the function overload findTxtField(theFormInQuestion). Or maybe you actually remove txtField from the form somewhere in your code.

Yngve Moe
  • 410
  • 2
  • 8
0

Possible

Double double;
int integer = (int) Math.round(double); <--

Make sure your double is initialized.

Manh Le
  • 1,630
  • 16
  • 26
  • The double is initialized and used SystemOut to test value. Int was the correct rounded value of the Double. I just can't get it to output in the textfield. I tested the string and it was also the correct value. – JSint Apr 15 '16 at 07:10
  • So I think the problem come from `findTxtField()`. Did you check it? – Manh Le Apr 15 '16 at 07:20
  • I created a TxtField on the same UI element "Reg" and I was able to output a message "Test", however I am not able to on other TxtFields on other UI Pages within the same application. – JSint Apr 15 '16 at 07:30
  • @JSint From you provided code, its hard to say where it come from. Did you set breakpoint on `findTxtField`? Also, please check your button that you click on is Null pointer or not. – Manh Le Apr 15 '16 at 08:33
  • No idea what the issue was. I rebuilt the application and can only assume there was a compatibility issue because the new build works fine. – JSint Apr 15 '16 at 17:02
0

I guess your error occurs because your variable name is double and since double is also a java keyword. Declare the variable like this:

double dbl = 0.0;

The second issue is that you dont have to cast your double to an integer and then to a string. That's nonesense. If you want to set the text just simply use:

findTxtField().setText(dbl + "");

That casts the double automatically. And if you want an integer value use:

findTxtField().setText((int) Math.round(dbl) + "");

If all that doesn't help you should check your findTxtField() method.

Spitzbueb
  • 5,233
  • 1
  • 20
  • 38
  • No the code was an example. The issue is setting the textfield to something. I tried findTxtField().setText(""); as a test and it threw up and error anyway without the string. That is nice code though, thanks. – JSint Apr 15 '16 at 07:19
  • Does your findTxtField() method return the right TextField? I think thats the issue. Could you probably post the methods code? – Spitzbueb Apr 15 '16 at 07:27
  • I created a TxtField on the same UI element "Reg" and I was able to output a message "Test", however I am not able to on other TxtFields on other UI Pages within the same application. I have never encountered this before. I have loads of code. Are you familiar with CodeNameOne? – JSint Apr 15 '16 at 07:33
  • No sorry, I just no Java. I Hope you'll find a solution. – Spitzbueb Apr 15 '16 at 07:59