Results 1 to 3 of 3
  1. #1
    yotamoo is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default Exception in thread "main" java.lang.NullPointerException

    So, i am trying to build a simple database (array of Car type).
    the code is quite simple but for some reason i keep getting the same error after running the program (compiling went through). so i have 3 classes:

    PHP Code:
    class DataBase {
        
        private Car[] database = new Car[3];
    
        public DataBase() {
        for (int i=0; i<database.length; i++) {
            database[i].formatCar();
        }
        
        } 
        
        public void addCar(Car car1) {
        for (int i=0; i<database.length; i++) {
            if (database[i].getModel()=="empty") {
            database[i]=car1;
            }
        }
        } 
    
        public void printDataBase() {
        int i=0;
        while (i<database.length) {
            database[0].printCar();
            i=i+1;
        }
        
        }
    
    }
    PHP Code:
    class mainpage {
    
        public static void main (String[] args) {
        Car car1 = new Car();
        DataBase database1 = new DataBase();
        car1.setCar();
         database1.addCar(car1);
        database1.printDataBase();
        }
    
    }
    PHP Code:
    import java.util.*;
    
    class Car {
    
        private String _model;
        private String _color;
        private int _year;
        private Scanner _input = new Scanner(System.in);
    
        public Car() {
    
        _model = "empty";
        _color = "empty";
        _year = 0;
    
        }
    
        public void setCar() {
    
        System.out.print("Enter model: ");
        _model = _input.next();
        System.out.print("Enter color: ");
        _color = _input.next();
        System.out.print("Enter year: ");
        _year = _input.nextInt();
    
        }
    
        public void formatCar() {
        _model = "empty";
        _color = "empty";
        _year = 0;
        }
    
        public String getModel() {
        String model=_model;
        return model;
        }
    
        public String getColor() {
        String color=_color;
        return color;
        }
    
        public int getYear() {
        int year=_year;
        return year;
        }
    
        public void printCar() {
        System.out.println("Model :"+_model);
        System.out.println("Color :"+_color);
        System.out.println("Year :"+_year);
        }
    
    
    }
    And the error i get:
    PHP Code:
    yotam@main computer:~/Documents/java/newcar$ java mainpage 
    Exception in thread "main" java.lang.NullPointerException
        at DataBase.<init>(DataBase.java:7)
        at mainpage.main(mainpage.java:5)
    yotam@main computer:~/Documents/java/newcar$
    i searched for possible reasons and i came across the possibility that the array wasn't initialized, but then i changed to constructor to set model and color to "empty" and year to 0.
    so now i have no clue, need your help guys.
    Thanks!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,875
    Rep Power
    25

    Default

    at DataBase.<init>(DataBase.java:7)
    The array element referenced at line 7 in DataBase is null.

    Arrays require special handling. You define how big they are to be and then you must put something into the empty slots. You have defined the array to have 3 slots, but they are empty until you assign a value to each slot.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,875
    Rep Power
    25

Similar Threads

  1. Replies: 7
    Last Post: 04-18-2011, 03:24 PM
  2. Replies: 3
    Last Post: 08-20-2009, 05:19 PM
  3. Replies: 6
    Last Post: 07-16-2009, 04:30 PM
  4. Replies: 1
    Last Post: 07-16-2009, 11:35 AM
  5. Replies: 1
    Last Post: 07-16-2007, 07: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
  •