Results 1 to 8 of 8
  1. #1
    Nitin Mundhe is offline Member
    Join Date
    Jan 2010
    Posts
    5
    Rep Power
    0

    Default 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.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    The exception tells you exactly where it occurs, so where is that?

  3. #3
    Nitin Mundhe is offline Member
    Join Date
    Jan 2010
    Posts
    5
    Rep Power
    0

    Default

    line no: 42
    (candidate.java:42)

  4. #4
    Nitin Mundhe is offline Member
    Join Date
    Jan 2010
    Posts
    5
    Rep Power
    0

    Default

    line no:42
    c[i].getData();

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    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.

  6. #6
    Nitin Mundhe is offline Member
    Join Date
    Jan 2010
    Posts
    5
    Rep Power
    0

    Default

    Thanks
    but i don't get it.
    please tell me what to do exactly.

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by Nitin Mundhe View Post
    line no:42
    c[i].getData();
    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();
    }

  8. #8
    Nitin Mundhe is offline Member
    Join Date
    Jan 2010
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Replies: 15
    Last Post: 10-12-2010, 05:51 PM
  2. Replies: 2
    Last Post: 01-15-2010, 01:30 AM
  3. Replies: 6
    Last Post: 07-16-2009, 03:30 PM
  4. Replies: 1
    Last Post: 07-16-2009, 10:35 AM
  5. Replies: 1
    Last Post: 07-16-2007, 06:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •