12 Replies - 10023 Views - Last Post: 04 January 2010 - 07:20 PM Rate Topic: -----

#1 jpete7683  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 80
  • Joined: 18-April 09

Exception in thread "Thread-0" java.lang.NullPointerExceptio

Post icon  Posted 29 December 2009 - 05:56 PM

I have posted about this project 1 other time. I had it working fine but my computer died and I lost the previous code. I have re-written it and all seems to be well except for my NULLPOINTEREXCEPTION!!!!!

I have tried to figure this out for probably 3 weeks so I decided to post it here. When I print to the stream (printwriter.println()) I get a null exception. I can just try printing a hard coded string and I still receive it. I do not see where I am either A.) Not initializing or B.) Object being null..... I went ahead and used System.out.println on the socket to make sure it wasn't null and it is infact not null. Please see the code below and offer suggestions if you have them.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import com.server.request.IncomingRequest;

public class ConnectionHandler extends Thread{

		private Socket socket = null;
		private IncomingRequest[] req;

		public ConnectionHandler(Socket socket, IncomingRequest[] _req){
				this.req = _req;
				this.socket = socket;
		}

		public void run(){
		BufferedReader bufferedReader = null;
		String msg = new String();
		PrintWriter printWriter;
		int rep = 0;

		try{
			bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			printWriter = new PrintWriter(socket.getOutputStream(),true);
			msg = bufferedReader.readLine();

			if(msg != null){
			   	rep = Integer.parseInt(msg);
			   	printWriter.println(req[rep].getRep());
			}
			printWriter.close();
			bufferedReader.close();
			System.runFinalization();
		}catch(IOException e){
				System.out.println(e);
		}
	  }
		
	  protected void finalize() throws Throwable {
		  try {
			  socket.close();
			  System.gc();
		  } finally {
			  super.finalize();
		  }
	  }

}



Is This A Good Question/Topic? 0
  • +

Replies To: Exception in thread "Thread-0" java.lang.NullPointerExceptio

#2 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 902
  • View blog
  • Posts: 3,163
  • Joined: 12-May 09

Re: Exception in thread "Thread-0" java.lang.NullPointerExceptio

Posted 29 December 2009 - 06:37 PM

Just throwing a suggestion out there - are you sure req isn't null and that req[whateverInt] isn't null?
Was This Post Helpful? 0
  • +
  • -

#3 jpete7683  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 80
  • Joined: 18-April 09

Re: Exception in thread "Thread-0" java.lang.NullPointerExceptio

Posted 29 December 2009 - 06:50 PM

View Postxclite, on 29 Dec, 2009 - 05:37 PM, said:

Just throwing a suggestion out there - are you sure req isn't null and that req[whateverInt] isn't null?


Yea I change req to just be a hard coded string and it still gave the null pointer. I ran it through Eclipse's debugger and I do see the correct string being assigned there.
Was This Post Helpful? 0
  • +
  • -

#4 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Exception in thread "Thread-0" java.lang.NullPointerExceptio

Posted 29 December 2009 - 07:34 PM

Where is the error pointing to?

Sorry, just seen where you mention it. The best way to solve this is to debug it yourself. Just chuck a println under everything where you can do a println. I often do this because somtimes, especially with null pointers, i find that the debugger points to the wrong line (I so have a faulty ide). But my guessing is that its deffinately to do with the socket.

Never used sockets before, but in that class, instead of assigning null to it, try assigning somthing real, and see if you still get the error. Maybe its an issue of where your assigning your socket.

This post has been edited by nick2price: 29 December 2009 - 07:39 PM

Was This Post Helpful? 0
  • +
  • -

#5 jpete7683  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 80
  • Joined: 18-April 09

Re: Exception in thread "Thread-0" java.lang.NullPointerExceptio

Posted 29 December 2009 - 08:21 PM

The exact line of code is

printWriter.println(req[rep].getRep());



I have put prints all over the place but it has something to do with the socket supposedly being null. I did a System.out.println(socket.getOutputStream) and it does not return a null instead just a mess of ugly characters which is what i would expect if it isn't null. I have added a screenshot of the debugger. In this case I called into the array to return "Not A Valid Request". You can see that in the top right pane of the screen. If i step any further into the code I will get the nullpointer exception and it occurs printwriter.java. Let me know if you have any other thoughts or if I should just start using a different way of sending strings across the wire to the client side. I don't want to do that because I want to know what the problem is before I just blow it all away.

Thanks for you help thus far

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#6 jpete7683  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 80
  • Joined: 18-April 09

Re: Exception in thread "Thread-0" java.lang.NullPointerExceptio

Posted 02 January 2010 - 12:01 PM

Any other ideas?
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Exception in thread "Thread-0" java.lang.NullPointerExceptio

Posted 02 January 2010 - 01:04 PM

Please post the result of this, placed before your current line

printWriter.println("req is " + req);


Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Exception in thread "Thread-0" java.lang.NullPointerExceptio

Posted 02 January 2010 - 01:58 PM

View Postjpete7683, on 2 Jan, 2010 - 11:01 AM, said:

Any other ideas?

The stack trace will give you the exact line where the null pointer exception occurs
if it is: printWriter.println(req[rep].getRep());

try:
System.out.println("Req is: " + req);
System.out.println("Rep is: " + rep);
System.out.println("Req[rep] is: " + req[rep]);
now if neither of them is null you can try:
System.out.println("Req[rep].getRep() is: " + req[rep].getRep());

your try/catch block is very large. You do not specificaly check for:
- the Integer.parseInt() on a non numeric message
- the size of the array according to rep
Was This Post Helpful? 0
  • +
  • -

#9 jpete7683  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 80
  • Joined: 18-April 09

Re: Exception in thread "Thread-0" java.lang.NullPointerExceptio

Posted 04 January 2010 - 06:21 PM

I inserted all of the code you gave and nothing was null



Req is: [Lcom.server.request.IncomingRequest;@4f037c71
Rep is: 0
Req[rep] is: com.server.request.IncomingRequest@6ddf073d
Req[rep].getRep() is: This is a valid request!!!


It seems like it is something in printwriter with java 1.6. Does anyone know of any issues with java 1.6 and printwriter?
Was This Post Helpful? 0
  • +
  • -

#10 jpete7683  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 80
  • Joined: 18-April 09

Re: Exception in thread "Thread-0" java.lang.NullPointerExceptio

Posted 04 January 2010 - 06:39 PM

I just re-compiled my whole project with 1.4_2 and it works. So what has changed? I will try 1.5 and see if it still works with that version.

Current version installed:
java version "1.6.0_0"
OpenJDK Runtime Environment (IcedTea6 1.6.1) (6b16-1.6.1-3ubuntu1)
OpenJDK 64-Bit Server VM (build 14.0-b16, mixed mode)


It worked with 1.5.0_21
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Exception in thread "Thread-0" java.lang.NullPointerExceptio

Posted 04 January 2010 - 06:58 PM

View Postjpete7683, on 4 Jan, 2010 - 05:39 PM, said:

I just re-compiled my whole project with 1.4_2 and it works. So what has changed? I will try 1.5 and see if it still works with that version.

Current version installed:
java version "1.6.0_0"
OpenJDK Runtime Environment (IcedTea6 1.6.1) (6b16-1.6.1-3ubuntu1)
OpenJDK 64-Bit Server VM (build 14.0-b16, mixed mode)


It worked with 1.5.0_21

The only thing new in PrintWriter 1.6 is the clearError() method
1.5 introduced the printf() and format() methods
You have a very interesting problem :) as you do notused any of these (wouldn't have compiled under 1.4)
Was This Post Helpful? 0
  • +
  • -

#12 Riveted  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 87
  • Joined: 03-December 09

Re: Exception in thread "Thread-0" java.lang.NullPointerExceptio

Posted 04 January 2010 - 07:13 PM

Your problem might actually be OpenJDK/IcedTea. Try uninstalling that, and installing the jdk/jre from here: http://java.sun.com/...loads/index.jsp. You may also have to go under the eclipse setting and change it to the correct jdk/jre once you do that.
Was This Post Helpful? 0
  • +
  • -

#13 jpete7683  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 80
  • Joined: 18-April 09

Re: Exception in thread "Thread-0" java.lang.NullPointerExceptio

Posted 04 January 2010 - 07:20 PM

I just used jdk1.6.0_05 and have the same results.

java version "1.6.0_05"
Java™ SE Runtime Environment (build 1.6.0_05-b13)
Java HotSpot™ 64-Bit Server VM (build 10.0-b19, mixed mode)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1