Frohes Neues,
ich habe ein kleines/mittelgroßes Problem mit Objekten. Vorweg: Ich bin Informatik-Student im ersten Semester und beim praktischen Teil gehts gerade mit Objekt-Orientierter Programmierung los.
Folgende Aufgabenstellung: Ich soll eine Buchklasse erstellen und dazu eine Bibliotheksklasse, in der ich die Bücher hinzufügen oder wieder entfernen kann. Da ich noch relativ am Anfang vom Studium bin, soll das (wohl) ohne irgendwelche Sachen, die man mit extrem viel Erfahrung irgendwann lernt funktionieren --> relativ einfach sein.
Ich poste einfach mal den Code, den ich soweit habe:
Klasse Book.java
Klasse Library.java:
Und meine Testklasse LibraryTest.java:
Die genaue Aufgabenstellung gibt es nochmal hier: Klick
Mein genaues Problem: Beim Ausführen gibt es in folgender Zeile in Library.java folgenden Laufzeitfehler:
<<Zeile 14 in Library.java
<< Zeile 6 in LibraryTest.java
Meine Fragen wären demzufolge:
Ich bin für jede erdenkliche Hilfe dankbar und hoffe auf schnelle Antwort!
ich habe ein kleines/mittelgroßes Problem mit Objekten. Vorweg: Ich bin Informatik-Student im ersten Semester und beim praktischen Teil gehts gerade mit Objekt-Orientierter Programmierung los.
Folgende Aufgabenstellung: Ich soll eine Buchklasse erstellen und dazu eine Bibliotheksklasse, in der ich die Bücher hinzufügen oder wieder entfernen kann. Da ich noch relativ am Anfang vom Studium bin, soll das (wohl) ohne irgendwelche Sachen, die man mit extrem viel Erfahrung irgendwann lernt funktionieren --> relativ einfach sein.
Ich poste einfach mal den Code, den ich soweit habe:
Klasse Book.java
Java:
public class Book {
private String title;
private String author;
private String genre;
private int year;
private int isbn;
public Book(String Title, String Author, String Genre, int Year, int Isbn)
{
title=Title;
author=Author;
genre=Genre;
year=Year;
isbn=Isbn;
}
public String toString()
{
String output;
output=title+", "+author+"\n"+year+"\n"+genre+"\n"+isbn;
return output;
}
}
Klasse Library.java:
Java:
public class Library {
private int cap;
private int capInc;
private int realAmount=0;
private Book [] books;
public Library(int initialCapacity, int capacityIncrement)
{
this.cap=initialCapacity;
this.capInc=capacityIncrement;
for(int i=0;i!=cap;i++)
books[i]=new Book("a","a","a",1,1);
System.out.println(books.length);
}
public void insert(Book b)
{
if(realAmount<cap) //there is still some space left
{
//Search for free space
for(int i=0;i!=cap;i++)
{
if(books[i]==null)
{
books[i]=b;
realAmount++;
return;
}
}
System.out.println("Error 0x001");
}
else if(capInc>0) //Try to increase space
{
cap+=capInc;
if(realAmount<cap) //there is still some space left
{
//Search for free space
for(int i=0;i!=cap;i++)
{
if(books[i]==null)
{
books[i]=b;
realAmount++;
return;
}
}
System.out.println("Error 0x002");
}
}
System.out.println("Error 0x003: Couldnt increase size");
}
public void delete(Book b)
{
for(int i=0;i!=cap;i++)
{
if(books[i]==b)
{
books[i]=null;
realAmount--;
return;
}
}
System.out.println("Error 0x002: Objekt nicht gefunden");
}
public void print()
{
for(int i=0;i!=cap;i++)
{
if(books[i]!=null)
{
books[i].toString();
}
}
}
}
Und meine Testklasse LibraryTest.java:
Java:
public class LibraryTest {
public static void main(String[] args) {
Library lib1=new Library(2,1);
Book book1=new Book("Buch1", "Autor1", "Genre1",2000,123456);
Book book2=new Book("Buch2", "Autor2", "Genre2",2001,123457);
Book book3=new Book("Buch3", "Autor3", "Genre3",2002,123458);
Book book4=new Book("Buch4", "Autor4", "Genre4",2003,123459);
Book book5=new Book("Buch5", "Autor5", "Genre5",2004,123450);
//Inserting a book in an empty library
lib1.insert(book1);
lib1.print();
//Inserting a book in an half-empty library
lib1.insert(book2);
lib1.print();
//Inserting a book in a full library which can be increased
lib1.insert(book3);
lib1.print();
//Creating a library which cant be increased
Library lib2=new Library(1,0);
lib2.insert(book4);
//Inserting a book in that full library
lib2.insert(book5);
lib2.print();
//Try to remove book1 from lib1
lib1.print();
lib1.delete(book1);
lib1.print();
//Try to remove book4 from lib1 which is not in lib1
lib1.delete(book4);
lib1.print();
//Create an empty library and try to remove a book from it
Library lib3=new Library(4,1);
lib3.delete(book2);
lib3.print();
//Do an explicit test of toString method from book.java
book1.toString();
}
}
Die genaue Aufgabenstellung gibt es nochmal hier: Klick
Mein genaues Problem: Beim Ausführen gibt es in folgender Zeile in Library.java folgenden Laufzeitfehler:
Java:
Exception in thread "main" java.lang.NullPointerException
at Library.<init>(Library.java:14)
at LibraryTest.main(LibraryTest.java:6)
Code:
books[i]=new Book("a","a","a",1,1);
Code:
Library lib1=new Library(2,1);
Meine Fragen wären demzufolge:
- Wieso ist dort eine NullException, wenn ich dieses Objekt doch gerade erzeugen will?
- Wie kann ich das Problem beheben?
- Gibt es noch eine andere Möglichkeit um entsprechend "nicht vorhandene" Objekte zu suchen anstatt dem (später folgenden) Abfragen auf Standardwerte?
Ich bin für jede erdenkliche Hilfe dankbar und hoffe auf schnelle Antwort!