This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes help with java. lang. NullPointer Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help with java. lang. NullPointer Exception" Watch "help with java. lang. NullPointer Exception" New topic
Author

help with java. lang. NullPointer Exception

Amit Ellana
Greenhorn

Joined: Mar 22, 2003
Posts: 6
please help i have been getting this error and i cant fix it
a lil background info:
This is my first major java project after HelloWorld, its suppose to be a Client Server program. I got the Client Server part to work with both echoing a string back and forth...Then i wanted to add something that would let the server know when a client is no longer available....so i add timers to both the server and the client...for the client timer i wanted it to send a "*" to server every 10 seconds.but when the client timer tries to send the "*" java.lang.NullPointerException.....please take a look at my code and tell me what i am doing wrong
im posting only the client codes cuz i feel that is what is causeing the problem CThreaded a threaded client, CScon connection class for the client, and CTimer a client timer.
//CThreaded

CScon

CTimer

the error message is:
java.lang.NullPointerException
at nwpj.CTimer.run(CTimer.java:97)

if you could please tell me what is causing this error or if you could atleaset explain the error to me i would be greatful..
Amit
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

No value is ever assigned to the member variable "silfa" in CTimer, so in CTimer.run, when you call a method on silfa, you get a null pointer exception.
silfa has to get initialized to point to a CScon object; you should probably do this in a constructor for CTimer, which would takethe CScon as a constructor argument.


[Jess in Action][AskingGoodQuestions]
Amit Ellana
Greenhorn

Joined: Mar 22, 2003
Posts: 6
Thanks for the reply Ernest Friedman-Hill but im not sure what the answer is? could you explain it a bit more or point me to a place where I could find that answer....again thanks for the help
amit
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

You would definitely benefit from reading some of the JavaRanch "campfire stories", here and especially here. If you read and understand both of these, then the answer to this problem should be obvious.
I'm going to move this thread to "Java in General (Beginner)" because the problem has nothing to do with Sockets, per se. Followups there, please.
[ November 08, 2003: Message edited by: Ernest Friedman-Hill ]
Amit Ellana
Greenhorn

Joined: Mar 22, 2003
Posts: 6
I have read the two articles above, but still can't resolve my problem. I need to create a constutor in CTimer but im still not sure what value to put into 'silfa'?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

CTimers are being created by code inside a method of CScon, so it's clear that a CTimer should have a reference to the particular CScon object that created it, right? So the CScon code should pass "this" to the CTimer constructor, which should store that argument into the member variable.
Inside any member method, the keyword "this" refers to the object on which the method was invoked.
Amit Ellana
Greenhorn

Joined: Mar 22, 2003
Posts: 6
thanks for the help....i think im a bit in over my head with this issue and i need to take a couple steps back and review.....
 
GeeCON Prague 2014
 
subject: help with java. lang. NullPointer Exception