• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NullpointerException

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Medes Agri
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes,

line 41 is s = MyServerSocket.accept();

s is socket.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Medes Agri
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, you are already running something on that port. probably another copy of the program. Perhaps you shud try using a different port?

John
 
reply
    Bookmark Topic Watch Topic
  • New Topic