java -nullpointer exception.

hai. i get null pointer exception on the blow code. the error is due t

Page 1 of 1

9 Replies - 872 Views - Last Post: 12 July 2009 - 09:12 PM Rate Topic: -----

#1 KirthikaRamkumar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-July 09

java -nullpointer exception.

Posted 12 July 2009 - 12:39 PM

class Details
{
 String name="a",designation="a";
 int salary=0,hra=0,da=0;
 int na,nb,nc;
 String changenamea,changenameb,changenamec;
 int netpay=0;


void get()
{
 DataInputStream dis=new DataInputStream(System.in);
 try
	{
		 System.out.println("Enter name:");
		 name = dis.readLine();
		 System.out.println("Enter designation:" );
		 designation = dis.readLine();
		 System.out.println("Enter salary:");
		 String salarystr = dis.readLine();
		 salary = Integer.parseInt(salarystr);
		 System.out.println("Enter HRA:");
		 String hrastr = dis.readLine();
		 hra = Integer.parseInt(hrastr);
		 System.out.println("Enter DA:");
		 String dastr = dis.readLine();
		 da = Integer.parseInt(dastr);
	}
 catch(IOException e)
	{
		System.out.println("Caught an error");
	  }
}
.....
....
}


class Employee
{
 public static void main(String args[])throws IOException
{
 try
{
 Details[] obj = new Details[25];
   
 DataInputStream br=new DataInputStream(System.in);
 int count,loopv=1,count1,validator=1;
 System.out.println("Enter employee details ");
 for(count = 1; loopv != 0; ++count)
 {
	  validator = count;
	  
 	obj[count].get();
	System.out.println("input captured");
 	obj[count].calc();	
	   
 	System.out.println("Do you wish to add details for another employee?enter 0 if no..");
	String temp = br.readLine();
	loopv = Integer.parseInt(temp);
 }
  for(count1 = 1; count1 <= validator; ++count1)
 {
 
	 obj[count1].put();
 }
 }
catch(NullPointerException e)
 {
	 System.out.println("Error:" + e);
 }

}
}


Please help me resolve the null pointer exception in this..
Thanks in advance..

*Edited to add the [ code] tags

This post has been edited by pbl: 12 July 2009 - 09:13 PM


Is This A Good Question/Topic? 0
  • +

Replies To: java -nullpointer exception.

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: java -nullpointer exception.

Posted 12 July 2009 - 12:48 PM

Could you re-post your question, since you put it in the Description field, and it got cut off?
Was This Post Helpful? 0
  • +
  • -

#3 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: java -nullpointer exception.

Posted 12 July 2009 - 12:51 PM

Before calling
obj[count].get()
you should initialize all of the objects in the array with
new Details()


Also Instead of
for(count = 1; loopv != 0; ++count)
start your loops with zero
for(count = 0; loopv != 0; ++count)

This post has been edited by syfran: 12 July 2009 - 12:56 PM

Was This Post Helpful? 0
  • +
  • -

#4 KirthikaRamkumar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-July 09

Re: java -nullpointer exception.

Posted 12 July 2009 - 12:58 PM

View Postsyfran, on 12 Jul, 2009 - 11:51 AM, said:

Before calling
obj[count].get()
you should initialize all of the objects in the array with
new Details()


Also Instead of
for(count = 1; loopv != 0; ++count)
start your loops with zero
for(count = 0; loopv != 0; ++count)


Hi ..

i have declared tht at the start of try..

the issue is i m getting a null pointer exception..

which is basically on the following line..

obj[count].get();
..

ya thanks i ll start the loop from 0..
Was This Post Helpful? 0
  • +
  • -

#5 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: java -nullpointer exception.

Posted 12 July 2009 - 01:03 PM

View PostKirthikaRamkumar, on 12 Jul, 2009 - 11:58 AM, said:

View Postsyfran, on 12 Jul, 2009 - 11:51 AM, said:

Before calling
obj[count].get()
you should initialize all of the objects in the array with
new Details()


Also Instead of
for(count = 1; loopv != 0; ++count)
start your loops with zero
for(count = 0; loopv != 0; ++count)


Hi ..

i have declared tht at the start of try..

the issue is i m getting a null pointer exception..

which is basically on the following line..

obj[count].get();
..

ya thanks i ll start the loop from 0..


You declared an arry of type Details but the array is full of null. Just try throwing in
obj[count] = new Details();
before the
obj[count].get();

This post has been edited by syfran: 12 July 2009 - 01:04 PM

Was This Post Helpful? 0
  • +
  • -

#6 KirthikaRamkumar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-July 09

Re: java -nullpointer exception.

Posted 12 July 2009 - 01:06 PM

View PostLocke, on 12 Jul, 2009 - 11:48 AM, said:

Could you re-post your question, since you put it in the Description field, and it got cut off?

Hi there is nuthing much i added there. it is jus that i m getting a null pointer exception on the following line..Do help me ressolve the same

obj[count].get();

..lik i have created a obj for this at the very start of try block..

y m i geeting a null pointer.. is there something wrong in the way i have called the get() using obj

..the basic ques here is.. to get , display/ calculate net pay of employees.. using array of objects..
Was This Post Helpful? 0
  • +
  • -

#7 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: java -nullpointer exception.

Posted 12 July 2009 - 01:11 PM

View PostKirthikaRamkumar, on 12 Jul, 2009 - 12:06 PM, said:

View PostLocke, on 12 Jul, 2009 - 11:48 AM, said:

Could you re-post your question, since you put it in the Description field, and it got cut off?

Hi there is nuthing much i added there. it is jus that i m getting a null pointer exception on the following line..Do help me ressolve the same

obj[count].get();

..lik i have created a obj for this at the very start of try block..

y m i geeting a null pointer.. is there something wrong in the way i have called the get() using obj

..the basic ques here is.. to get , display/ calculate net pay of employees.. using array of objects..


At the start of the try block you created an array that can hold Details. You still need to put the Details object in there.

Until you do the array will be filled will null and obj[count].get() will be calling null.get() which produces a null pointer exception.

Then solution is to create new Details objects to put in each slot of the array. The ideal location would be just before the obj[count].get
Was This Post Helpful? 0
  • +
  • -

#8 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: java -nullpointer exception.

Posted 12 July 2009 - 01:11 PM

Look at what syfran has told you to do. You never initialized any part of the array.
Was This Post Helpful? 0
  • +
  • -

#9 KirthikaRamkumar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-July 09

Re: java -nullpointer exception.

Posted 12 July 2009 - 01:21 PM

View Postsyfran, on 12 Jul, 2009 - 12:03 PM, said:

View PostKirthikaRamkumar, on 12 Jul, 2009 - 11:58 AM, said:

View Postsyfran, on 12 Jul, 2009 - 11:51 AM, said:

Before calling
obj[count].get()
you should initialize all of the objects in the array with
new Details()


Also Instead of
for(count = 1; loopv != 0; ++count)
start your loops with zero
for(count = 0; loopv != 0; ++count)


Hi ..

i have declared tht at the start of try..

the issue is i m getting a null pointer exception..

which is basically on the following line..

obj[count].get();
..

ya thanks i ll start the loop from 0..


You declared an arry of type Details but the array is full of null. Just try throwing in
obj[count] = new Details();
before the
obj[count].get();


thanks a lot syfran...it worrks fine..
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

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

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

Re: java -nullpointer exception.

Posted 12 July 2009 - 09:12 PM

and please respect rule #4 of this forum that was showed to you when you did your original post
:code:
I'll edit your first post... this time :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1