I have a null point exception but I am not sure why. I am writing a code that is trying to recive an output from the server and then change something in my app. However I am getting the null pointer and not sure why. My update method seems to be giving the error and "if(test.result())" seems to be the line of code that is giving me the problem. Here are the part of my code that I think would be relevant. If I need to give me code, I have no problem giving it.
public class SpaceInvadersActivity extends Activity{
InvaderClass InvaderListen;
Map game2 = new Map(spaceInvadersView,InvaderListen);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
InvaderListen = new InvaderClass(hostName, portnum,response);
spaceInvadersView = new SpaceInvadersView(this, size.x,size.y,InvaderListen.returned());
InvaderListen.execute();
game2 = new Map (spaceInvadersView, InvaderListen);
setContentView(game2.getView());
}
}
SpaceInvadersView extends SurfaceView implements Runnable{
InvadersListener testListen;
public SpaceInvadersView(Context context, int x, int y,InvadersListener testing) {
super(context);
this.testListen = testing;
}
private void update(){
if(testListen.result()){ //So I guess the value returned by .result is
//null when it should not be
*does the changes I want*
}
}
l
public class InvaderClass extends AsyncTask<Void, Void, Void> {
*instance variables*
InvaderClass(String addr, int port,TextView textResponse) {
dstAddress = addr;
dstPort = port;
this.textResponse = textResponse;
}
public InvaderClass(String hostname) {
// Try and connect to the server.
System.out.println(hostname + portnum);
try {
socket = new Socket(hostname, portnum);
} catch (IOException ioe) {
System.out.println("There was an error connecting to the server: " + ioe);
System.exit(-1);
}
try {
output = new ObjectOutputStream(socket.getOutputStream());
input = new ObjectInputStream(socket.getInputStream());
} catch (IOException ioe) {
System.out.println("Exception creating I/O streams: " + ioe);
}
listener = new InvadersListener(input);
listener.start();
}
public boolean reader(){
return listener.result();
}
public InvadersListener returned(){return listener;}
@Override
protected Void doInBackground(Void... arg0) {
InvaderClass chat = new InvaderClass(hostName);
return null;
}
Console
04/02 14:37:11: Launching app
Cold swapped changes.
$ adb shell am start -n
"com.example.lbillym.space/com.example.lbillym.space.SpaceInvadersActivity"
-a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 15135 on device Nexus_5X_API_25 [emulator-5554]
I/art: Not late-enabling -Xcheck:jni (already on)
W/art: Unexpected CPU variant for X86 using defaults: x86
[ 04-01 20:14:06.974 1539: 1564 D/ ]
HostConnection::get() New Host Connection established 0x8b9df380, tid
1564
W/System: ClassLoader referenced unknown path:
/data/app/com.example.lbillym.space-1/lib/x86
I/InstantRun: Instant Run Runtime started. Android package is
com.example.lbillym.space, real application class is null.
W/System: ClassLoader referenced unknown path:
/data/app/com.example.lbillym.space-1/lib/x86
W/art: Suspending all threads took: 5.328ms
[ 04-01 20:14:07.877 15135:15160 D/ ]
HostConnection::get() New Host Connection established 0x9d8e5040, tid
15160
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
I/art: Background sticky concurrent mark sweep GC freed 9050(1149KB)
AllocSpace objects, 4(2MB) LOS objects, 47% free, 4MB/8MB, paused 8.249ms
total 120.767ms
[ 04-01 20:14:07.914 15135:15159 D/ ]
HostConnection::get() New Host Connection established 0xa409d580, tid
15159
E/EGL_emulation: tid 15160: eglSurfaceAttrib(1174): error 0x3009
(EGL_BAD_MATCH)
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x919a0ca0,
error=EGL_BAD_MATCH
D/gralloc_ranchu: gralloc_unregister_buffer: exiting HostConnection (is
buffer-handling thread)
E/AndroidRuntime: FATAL EXCEPTION: Thread-4
Process: com.example.lbillym.space, PID: 15135
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.example.lbillym.space.InvadersListener.result()' on a null object reference
at com.example.lbillym.space.SpaceInvadersView.update(SpaceInvadersView.java:159)
at com.example.lbillym.space.SpaceInvadersView.run(SpaceInvadersView.java:131)
at java.lang.Thread.run(Thread.java:761)