4 Replies - 425 Views - Last Post: 22 October 2011 - 12:21 AM Rate Topic: -----

#1 AuroraMirai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-October 11

Help with (Exception in thread "main" java.lang.NullPointerEx

Posted 21 October 2011 - 11:29 PM

I am sorry if I can't spot any obvious mistake, I am very new to programming so please be considerate.

I have this error when I run my program, and it's driving me crazy because I can't spot the mistake.
Below is my code

 public static void addinfo(int range,int start){
        
        String tittle;
        Person info[] = new Person[range]; /*Reintiliazing class*/ 
        Scanner kb = new Scanner (System.in); /*Make new scanner for input*/
        System.out.println("\n Enter a student tittle (eg. Mr or Mrs)");
        tittle = kb.nextLine();
        info[0].GetT("Mr");
        
        
        
                
        
    }


and my Class
class Person {
    private String tittle;
    private String name;
    private String dob;
    
    public void GetT(String s)
    {
          tittle = s;
  
    } 


What i'm trying to do here is to make an array of classes. But it returns Exception (in thread "main" java.lang.NullPointerException). Can anyone please help me?

thanks for reading

Is This A Good Question/Topic? 0
  • +

Replies To: Help with (Exception in thread "main" java.lang.NullPointerEx

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Help with (Exception in thread "main" java.lang.NullPointerEx

Posted 21 October 2011 - 11:54 PM

You have many problems here,
First your array is declared inside a method, which means everytime you call a method it is created a new one which is empty. This means your array is empty and has no any person, also even that new array, it is empty at index 0 since you have not created any instance of person. Something like info[0] = new Person(); and so at least you will have one instance of person at index 0 which will be dead after the method end
Was This Post Helpful? 0
  • +
  • -

#3 AuroraMirai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-October 11

Re: Help with (Exception in thread "main" java.lang.NullPointerEx

Posted 21 October 2011 - 11:59 PM

Thank you for your respond but the program still return me an error after I moved it into the main section.

public class Main {   /*the begining of the program*/


    public static void main(String[] args) {
        int finish = 0;      /*Set int finish to end the loop when a user chose to quit*/
        int range = 10;         /*Set the limit of array data base (Can be manipulated in one of the option*/
        int start = -1; /*Set the starting point of the database -1 because the script will always +1 by default so it starts from 0*/
        char choice;     /*Set string for menu*/
        Scanner input = new Scanner (System.in);        /*Initializing scanner for inputs*/
        System.out.println(StudentInfo());               /*Print The Authour Information*/
        Person info[] = new Person[range]; /*Create new Class Person to use for database*/ 
        do{
            ShowMenu();           /*Show Menu*/
            choice = getChoice(); /*Get Option from the user and convert it to char to use in switch case*/
            switch (choice)
            {
                case 'a':
                    System.exit(0);   /*Option 1. Exit Program*/
                case 'b':
                {
                    String tittle;
                    Scanner kb = new Scanner (System.in); /*Make new scanner for input*/
                    System.out.println("\n Enter a student tittle (eg. Mr or Mrs)");
                    tittle = kb.nextLine();
                    System.out.println(info[0].GetT(tittle));
        
                }
                    
            }

        }while(finish == 0);


    }


It still returns the same error, what is the actual pipe line of making an array of classes?
and again, thx for the reply.
Was This Post Helpful? 0
  • +
  • -

#4 AuroraMirai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-October 11

Re: Help with (Exception in thread "main" java.lang.NullPointerEx

Posted 22 October 2011 - 12:14 AM

My bad for not being very observant. It is now working after I added info[0] = new Person();

Thanks a lot!
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Help with (Exception in thread "main" java.lang.NullPointerEx

Posted 22 October 2011 - 12:21 AM

Glad you get it, so for every index you will need to add a person otherwise it will be null at that point
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1