Results 1 to 8 of 8
- 01-20-2010, 10:54 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 5
- Rep Power
- 0
Exception in thread "main" java.java.NullPointerException at CandiadateStore.main
(candidate.java:42)
while executing program this error is occured
please help
//program code
import java.io.*;
public class Candidate
{
int id;
String name;
String qualification;
int experience;
void getData()
{
try
{
DataInputStream in = new DataInputStream(System.in);
System.out.println("Enter candidate id:");
System.out.flush();
String idString = in.readLine();
id = Integer.parseInt(idString);
System.out.println("Enter candidate name:");
name = in.readLine();
System.out.println("Enter Qulification");
qualification = in.readLine();
System.out.println("Enter experience");
String experienceString = in.readLine();
experience = Integer.parseInt(experienceString);
}catch(Exception e){}
}
void putData()
{
System.out.println("Candidate id:"+id);
System.out.println("Candidate Name"+name);
System.out.println("Candidate Qualification"+qualification);
System.out.println("Candidate Experience"+experience);
}
}
class CandidateStore
{
public static void main(String args[])
{
Candidate[] c =new Candidate[3];
for(int i=0; i<c.length - 1; i++)
{
c[i].getData();
}
for(int j=0; j< c.length - 1; j++)
{
c[j].putData();
}
}
}Last edited by Nitin Mundhe; 01-20-2010 at 11:16 AM.
- 01-20-2010, 11:02 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
The exception tells you exactly where it occurs, so where is that?
- 01-20-2010, 11:05 AM #3
Member
- Join Date
- Jan 2010
- Posts
- 5
- Rep Power
- 0
line no: 42
(candidate.java:42)
- 01-20-2010, 11:06 AM #4
Member
- Join Date
- Jan 2010
- Posts
- 5
- Rep Power
- 0
line no:42
c[i].getData();
- 01-20-2010, 11:09 AM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Never do this catch(Exception e){}.
At least print the stack trace. Find the reported line in your code.
One of the variables being accessed at that line is null.
Use printlns to see what values your variables point to.
- 01-20-2010, 11:21 AM #6
Member
- Join Date
- Jan 2010
- Posts
- 5
- Rep Power
- 0
Thanks
but i don't get it.
please tell me what to do exactly.
- 01-20-2010, 11:22 AM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
If that is the line then either c is null, or no element is stored under c[i]. You do know that declaring an array with a reference type (i.e. Object a = new Object[2]) does not create any objects, but rather gives each element the reference value "null", right? You need to explicitly create the object in each element i.e.
Java Code:Object a = { new Object(), new Object() }; // or Object a = new Object[2]; for (int i = 0; i < a.length; i++) { a[i] = new Object(); }
- 01-20-2010, 11:35 AM #8
Member
- Join Date
- Jan 2010
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Exception in thread "main" java.lang,NullPointerException
By ljk8950 in forum AWT / SwingReplies: 15Last Post: 10-12-2010, 05:51 PM -
Exception in thread "main" java.lang.NullPointerException
By Rohaan in forum New To JavaReplies: 2Last Post: 01-15-2010, 01:30 AM -
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 6Last Post: 07-16-2009, 03:30 PM -
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 1Last Post: 07-16-2009, 10:35 AM -
ArrayList: Exception in thread "main" java.lang.NullPointerException
By susan in forum New To JavaReplies: 1Last Post: 07-16-2007, 06:32 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks