wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes y am i getting null pointer exception here? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "y am i getting null pointer exception here?" Watch "y am i getting null pointer exception here?" New topic
Author

y am i getting null pointer exception here?

krishna balaji
Ranch Hand

Joined: Mar 16, 2006
Posts: 31
this is the code and i am null pointer exception


[ EFH: Fix code tags ]
Wat can b various reasons for getting null pointer exceptions
[ October 30, 2006: Message edited by: Ernest Friedman-Hill ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

NullPointerException always means that you've tried to access an object that simply isn't there; i.e., a variable's value is null instead of referring to an object. In general, it's a variable followed by a dot (or an open square bracket, if it's an array.)

This is far too much code for me to look at, but if you look at your stack trace it will tell you what line number the exception occurs on. If you then indicate to us exactly which line of code that is, I'm sure we can help you out.


[Jess in Action][AskingGoodQuestions]
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
You also need to use code tags. If you look at your code after you post, and your code is a mess, then you need to do something about it if you want people to answer your question.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by sven studde:
You also need to use code tags.


To be fair: he did, they were just broken. I fixed them.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Do you use an IDE? If so, just check at which line the NullPointerException occurs.
krishna balaji
Ranch Hand

Joined: Mar 16, 2006
Posts: 31
[code]
confserver obj[];
//i am getting null pointer error in the following line wats the problem
obj[i].m=new messageQ();
obj[i].g.start();
obj[i].s.start();
[code]

confserver is class in defined how to create array of the class ?
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

if the code compiles, the confserver object array must be non local.
Most probably the array is empty. If it is non local (i.e. an instance variable) it even may be not initialized.
In both cases (empty or uninitialized) a null pointer exception is thrown.


Simple example


Prints null
and then throws NullPointerException.


By the way, Krishna, you should give classes identifiers that start with an uppercase letter. Makes the code easier to read (for humans, not for compilers).

krishna balaji wrote
confserver is class in defined how to create array of the class ?
confserver obj[];
It is declared but not defined and filled yet, not even an empty array, but a reference to a confserver array.
Please put the brackets behind the class identifier
confserver[] obj;
would be the normal way to say this. The way you did it works, but is unusual and also harder to read.
obj is not a nice name for a variable. It tells you nothing.

Declare, initialize and fill an array, example:




Yours,
Bu.


all events occur in real time
krishna balaji
Ranch Hand

Joined: Mar 16, 2006
Posts: 31
Thank you sir that was very informative now i understand the problmem
 
wood burning stoves
 
subject: y am i getting null pointer exception here?
 
Similar Threads
Hashmap in android
Java - Works, but doesnt work...
response without request
using public class in other packages
MySQLException