Hello everyone I'm new to java and this forum and I have a problem that I need to solve:
I've created a program with a class and a main, which is this:
CLASS BOOK:
MAIN:
I get this message:
Exception in thread "main" java.lang.NullP ointerException
at Askhsh4.Askhsh4 .main(Askhsh4.j ava:21)
Java Result: 1
Thanks everyone in advance!
I've created a program with a class and a main, which is this:
CLASS BOOK:
Code:
public class Book {
private String title;
private String author;
private int FirstEdition;
public Book(){
title="";
author="";
FirstEdition=0;
};
public void SetTitle(String a){
this.title=a;
}
public void SetAuthor(String a){
this.author=a;
}
public void SetFirstEdition(int a){
this.FirstEdition=a;
}
public String GetTitle(){
return this.title;
}
public String GetAuthor(){
return this.author;
}
public int GetFirstEdition(){
return this.FirstEdition;
}
};
MAIN:
Code:
public static void main(String[] args) {
int i;
Book [] a= new Book[2];
for(i=0;i<2;i++){
a[i].SetTitle("1984");
a[i].SetAuthor("Orwell");
a[i].SetFirstEdition(1980);
}
for(i=0;i<2;i++){
System.out.println("Book Title:"+a[i].GetTitle());
System.out.println("\nAuthor Name:"+a[i].GetAuthor());
System.out.println("\nFirst Edition:"+a[i].GetFirstEdition());
}
}
I get this message:
Exception in thread "main" java.lang.NullP ointerException
at Askhsh4.Askhsh4 .main(Askhsh4.j ava:21)
Java Result: 1
Thanks everyone in advance!
Comment