-5

Why am I getting errors??? I use eclipse, the error

Exception in thread "main" java.lang.NullPointerException at Shopping_Booket.Movie_assign(Shopping_Booket.java:8) at AcmeVideo.main(AcmeVideo.java:8)

The code:

public class AcmeVideo {

    public static void main(String[] args) {
        Shopping_Booket shop = new Shopping_Booket();
        shop.Movie_assign();
    }
}

public class Shopping_Booket {

    movie[] MovieArray = new movie[5];

    public void Movie_assign() {

        MovieArray[0].Assign("Batman Dark Knight Return", 8, 2012, 22);
        MovieArray[1].Assign("Fringe", 9.2, 2008, 40);
        MovieArray[2].Assign("V for Vandetta", 8, 2005, 28);
        MovieArray[3].Assign("X man First Class", 7, 2005, 30);
        MovieArray[4].Assign("Hulk", 8.5, 2000, 16);
        System.out.println(MovieArray[2].Title);
    }
}

public class movie {

    public String Title;
    public double Rating;
    public int Year;
    public double Price;

    public void Assign(String title, double rating, int year, double price) {
        this.Title = title;
        this.Rating = rating;
        this.Year = year;
        this.Price = price;
    }

    public void Displayİnfo() {
        System.out.println("Title = " + Title);
        System.out.println("Year = " + Year);
        System.out.println("Rating = " + Rating);
        System.out.println("Price = " + Price);
        System.out.println("-------------------------------------");
    }
}
Lion
  • 18,729
  • 22
  • 80
  • 110
cengiz
  • 101
  • 1
  • 7
  • http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html – Brian Roach Apr 27 '13 at 15:26
  • Please don't use class names like `Shopping_Booket`, `movie`. Method names like `Movie_assign()`. They don't confirm the Java naming convention. – Lion Apr 27 '13 at 15:36

2 Answers2

4

You have created an array, but you have never created instances. Your array is simply an array of null objects.

movie [] MovieArray=new movie [5];

Currently movie looks like this:

{null, null, null, null, null}

And what you need to do is add some instances to it.

MovieArray[0] = new Movie(// etc);

Just to note

The Java naming conventions state that variables that are not constants must begin with a lower case letter, and all subsequent words start with an upper case letter.

MovieArray -> movieArray
christopher
  • 26,815
  • 5
  • 55
  • 89
3

This declaration:

movie [] MovieArray=new movie [5];

... creates a new array with 5 elements, but each element is null to start with. You never actually create a new instance of the movie() class. Instead, you're calling your Assign method on a null reference each time - and that's what's causing the exception.

See the Arrays part of the Java tutorial for a bit more information on arrays.

You should change your Assign method into a constructor, and then write:

MovieArray[0] = new movie("Batman Dark Knight Return", 8, 2012, 22);

You should then definitely read up on Java naming conventions:

  • Your class should be Movie, not movie
  • Your variables should start with a lower case letter (title, not Title)
  • Ditto your methods: displayInfo etc

I'd also strongly advise you to use private fields instead of public ones.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194