wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes NullpointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NullpointerException" Watch "NullpointerException" New topic
Author

NullpointerException

Medes Agri
Greenhorn

Joined: Jul 07, 2006
Posts: 11
Hi,

I have created a server application (very simple) when i run it by using JCreator it goes well and it wates untill a client connect on it.

for the first time i used NetBeans and I made my first Java Application I added my Server to the project and removed the main method of the server.

the main method of the Main class of my project looks like this.

public static void main(String[] args)
{
int port = 7500;
MyServer MS = new MyServer(port);
MS.accept();
}

when i compile it , it goes well and it does not generate any error but when i run my project i get following error:

Exception in thread "main" java.lang.NullPointerException
at MyPackage.MyServer.accept(MyServer.java:41)


thanks for your help
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

The error message says that the problem is in the accept() method of the MyServer class, at line 41 of MyServer.java. Let's see that method, and tell us which is line 41; the problem will probably be obvious.


[Jess in Action][AskingGoodQuestions]
Medes Agri
Greenhorn

Joined: Jul 07, 2006
Posts: 11
yes,

line 41 is s = MyServerSocket.accept();

s is socket.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

OK, well, the error means that MyServerSocket is null -- i.e., the actual ServerSocket object is not being created. Where does it get initialized? Is the code that initializes it still going to run in your new configuration?
Medes Agri
Greenhorn

Joined: Jul 07, 2006
Posts: 11
Thanks,

here is MyServer


public class MyServer
{
private ServerSocket MyServerSocket = null;
ArrayList words = new ArrayList();
ArrayList numbers = new ArrayList();

public MyServer(int port)
{
try
{
MyServerSocket = new ServerSocket(port);
}catch(IOException e){}
}

public void accept()
{
try
{
System.out.println("Server 1");
Socket s;
int ThreadId = 0;
while(true)
{
s = MyServerSocket.accept();
ThreadId++;


and I have called MyServer as i wrote in my first topic
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40



Don't just ignore the IO exception. If something goes wrong with the instantiation -- such as another app is already using that port. The MyServerSocket will still be null.

Henry
[ July 10, 2006: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Medes Agri
Greenhorn

Joined: Jul 07, 2006
Posts: 11
thank you,

I changed to catch(Exception e)instead.
and moved MyServerSocket = new ServerSocket(port); to the accept method

but now it generates


FAILED TO HOST ON 7500
[ July 10, 2006: Message edited by: Medes Agri ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

This:

}catch(IOException e){}


Is about the worst thing you can do, especially as a beginner. An exception is a message that tells you about something important that's happening; an empty catch block tears that message up without even looking at it. At the very least, you want to say

} catch (IOException e) { e.printStackTrace(); }

In any case, since that variable is null, I can guarantee you that this will print something out, and it's going to tell you exactly what the problem is!
Jody Brown
Ranch Hand

Joined: Nov 09, 2005
Posts: 43
Changing the Exception type wont alter a thing - there is still a fundamental reason why your attempt to create an instance of ServerSocket is failing. in your catch block, try the following, and post the results it displays here for us to take a look at:

John Bartlett
Ranch Hand

Joined: Jan 25, 2006
Posts: 116
Hi there,

I believe what Henry means, is that you need to handle the exception if it is thrown. All you are doing is throwing the exception you are not actually saying what you want done incase of the exception.

Perhaps something like this:



I may be wrong, but worth a try i suppose?

Regards,

John
Medes Agri
Greenhorn

Joined: Jul 07, 2006
Posts: 11
I used catch(IOException) {e.printStackTrace();

and the result is

init:
deps-jar:
Compiling 3 source files to C:\JavaProjects\OneClientTwoServer\build\classes

Note: Recompile with -Xlint:unchecked for details.
compile:
run:
java.net.BindException: Address already in use: JVM_Bind
Server 1
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:359)
at java.net.ServerSocket.bind(ServerSocket.java:319)
at java.net.ServerSocket.<init>(ServerSocket.java:185)
at java.net.ServerSocket.<init>(ServerSocket.java:97)
at oneclienttwoserver.ServerOne.<init>(ServerOne.java:22)
at oneclienttwoserver.Main.main(Main.java:30)
FAILED TO HOST ON 7500
BUILD SUCCESSFUL (total time: 0 seconds)

how it could use port 7500 when it generated exceptions always
[ July 10, 2006: Message edited by: Medes Agri ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

OK, well, there you go. Only one program can listen on a given port at a time, but the error says another program is already using your port 7500:

java.net.BindException: Address already in use: JVM_Bind

Perhaps you just have another copy of your code still running.
John Bartlett
Ranch Hand

Joined: Jan 25, 2006
Posts: 116
yes, you are already running something on that port. probably another copy of the program. Perhaps you shud try using a different port?

John
 
 
subject: NullpointerException