Object should first be initialized & java.lang.NullPointerException

PharCyDeD

Active Member
Licensed User
Longtime User
In my app I compile fine, but then I get paused on certain lines in my code during running it. First it tells me:

Object should fist be initialized (ListView)

I am not sure it is is correct, but I put:

B4X:
ListView1.Initialize(ListView1)

The ListView1 is names ListView1 and the Event Name is ListView1 as well. It let me pass that in the next run it pauses then highlights this:

B4X:
If Blue1Location = 15 Then
      Blue1.Top = 147
      Blue1.Left = 677
   End If

It highlights the line 'Blue1.Top = 147' and says "Object should first be initialized (ImageView). The ImageView is named Blue1 and the Event Name is Blue1 as well so I thought the same as above and did.

B4X:
Blue1.Initialize(Blue1)

It then runs through and says "Last Exception java.lang.NullPointerException" under Local Variables


Here is my log:

** Activity (main) Create, isFirst = true **


** Activity (main) Resume **


** Activity (main) Pause, UserClosed = false **


** Activity (gamescreen1) Create, isFirst = true **


** Activity (main) Create, isFirst = true **


** Activity (main) Resume **


** Activity (main) Pause, UserClosed = false **


** Activity (gamescreen1) Create, isFirst = true **


gamescreen1_setblue1location (B4A line: 892)


Blue1.Top = 147

java.lang.NullPointerException
at anywheresoftware.b4a.objects.ViewWrapper.setTop(ViewWrapper.java:139)
at triangle.puzzler.gamescreen1._setblue1location(gamescreen1.java:4895)
at triangle.puzzler.gamescreen1._generatelocations(gamescreen1.java:2099)
at triangle.puzzler.gamescreen1._activity_create(gamescreen1.java:617)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:113)
at triangle.puzzler.gamescreen1.afterFirstLayout(gamescreen1.java:84)
at triangle.puzzler.gamescreen1.access$100(gamescreen1.java:16)
at triangle.puzzler.gamescreen1$WaitForLayout.run(gamescreen1.java:72)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)


at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
java.lang.NullPointerException

What is strange to me is that in my test program to see if my method would work I didn't have to initialize the ListView or ImageView...only the Map. Now I am forced to initialize them and then get the NullPointException error.

:BangHead:

:sign0163:
 

klaus

Expert
Licensed User
Longtime User
If the views are loaded with a layout file you must not initialize the views.
If you add views in the code you must initialize them.
But
B4X:
ListeView1.Initialize(ListeView1)
Blue1.Initialize(Blue1)
must be
B4X:
ListeView1.Initialize("ListeView1")
Blue1.Initialize("Blue1")
Best regards.
 
Upvote 0

PharCyDeD

Active Member
Licensed User
Longtime User
Views were placed in the program with the Designer and loaded with the layout I assume. I did change the code to include " ", but I am still getting the NullPointerException error regardless. My small test program seems to work fine, but it is of course not on the scale of my current app. Any thoughts on why the null error is popping up?
 
Upvote 0

PharCyDeD

Active Member
Licensed User
Longtime User
Also, since my Views were added in the designer and loaded with the layout...why is it forcing me to initialize them? It seems to me that the problem lies somewhere around that...but I am pretty puzzled right now.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Are you sure that the spelling is exactly the same as in the Designer ?
As already said, if the views are defined in the layout you must NOT initialize them.
Did you dim the the views?
Otherwise you should post your project as a zip file so we could look at it and find whats wrong. It's difficult to find the problem without seeing the whole project.

Best regards.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Unfortunately there are layout files missing in the project you poseted so I can't test it.
From what I've seen you should put
B4X:
Activity.LoadLayout("MatchRound1")
at the beginning of the Activity_Create routine.

Best regards.
 
Upvote 0

PharCyDeD

Active Member
Licensed User
Longtime User
Activity.LoadLayout("MatchRound1") adding that at the beginning solved my issue. I thought it could be called anywhere throughout that (it was at the bottom), but once I moved to the top...it worked. Thanks
 
Upvote 0
Top