Ho un esercizio da svolgere, devo creare una libreria nella quale sia possibile inserire nuovi libri. Ho un vettore vuoto e quando vado a fare la scansione con il ciclo for dei vettori disponibili mi da la null pointer exc. Come posso risovere il problema?
ps sono alle prime armi con il java e ho poca esperienza
codice maincodice:public class book { private String Author; private String Title; private String Editor; private String IdLoan; private String DateLoan; private int year; boolean presence; book(String au, String tit, String ed,int yr, String id, String Dm, boolean pr){ this.Author=au; this.Title=tit; this.Editor=ed; this.year=yr; this.IdLoan=id; this.DateLoan=Dm; this.presence=pr; } public String getAuthor() { return Author; } public void setAuthor(String au) { this.Author = au; } public String getTitle() { return Title; } public void setTitle(String title) { this.Title = title; } public String getEditor() { return Editor; } public void setEditor(String Ed) { this.Editor = Ed; } public int getYear() { return year; } public void setYear(int ye) { this.year=ye; } public String getIdMoan(String id) { return IdLoan; } public void setIdLoan(String id) { this.IdLoan = id; } public String getDateMoan() { return DateLoan; } public void setDateLoan(String Dm) { this.DateLoan = Dm; } public boolean getPresence() { return presence; } public void setPresence(boolean pr) { this.presence = pr; } static void menu(){ System.out.println("Gestione libreria"); System.out.println(" "); System.out.println("Scegliere tra le seguenti opzioni"); System.out.println("1. Inserire nuovo libro"); System.out.println("2. Visualizzazione lista libri"); System.out.println("3. Ricerca per editore"); System.out.println("4. Ricerca libri in prestito"); System.out.println("5. Visualizza numero libri disponibili"); } static void Insert (String a){ System.out.println("Inserire nuovo "+a); } static void auth (book [] x, String a){ for(int i=0; i<x.length; i++){ if(x[i].Author==null)x[i].Author=a; } } static void tit (book [] x, String a){ for(int i=0; i<x.length; i++){ if(x[i].Title== null)x[i].Title=a;} } static void edit (book [] x, String a){ for(int i=0; i<x.length; i++){ if(x[i].Editor== null)x[i].Editor=a;} } static void year (book [] x, int a){ for(int i=0; i<x.length; i++){ if(x[i].year == 0)x[i].year=a; } } static void idLoan (book [] x, String a){ for(int i=0; i<x.length; i++){ if(x[i].IdLoan== null)x[i].IdLoan=a;} } static void dateLoan(book [] x, String a){ for(int i=0; i<x.length; i++){ if(x[i].DateLoan.equals(null))x[i].DateLoan=a; } } static void presence(book [] x, boolean a){ for(int i=0; i<x.length; i++){ if(x[i].presence==null)x[i].presence=a; } } static void list(book [] x){ for ( int i = 0; i <x.length; i++){ System.out.print("\t"+x[i].Author+"\t"+x[i].Title+"\t"+x[i].Editor+"\t"+x[i].year+"\t"+x[i].IdLoan+"\t"+x[i].DateLoan+"\t"+x[i].presence); } } static void repilog (book [] x){ System.out.println(" I dati inseriti sono :"); System.out.println(" "); for (int i = 0; i<x.length; i++){ System.out.println(" "+x[i]); } } static void compare(book []x , String a){ for(int i =0 ; i<x.length; i++){ x[i].Editor.compareTo(a); System.out.println("Dati:\n"+x[i]); } } static void compare2(book [] x, String a, String b){ } static void number(book []x, int n){ for(int i = 0; i<x.length; i++){ if(x[i].presence = true)n= n+1; } System.out.println(" numero di libri disponibili è:"+n); } }
edit reso la parte codice più visibile.codice:/*1. creazione di un array * 2. Visualizzazione dell'array in forma tabellare * 3. visualizzare i dati dei libri editi da un editore scelto da un utente * 4. Determinare il numero dei libri che sono stati forniti in prestito da un intervallo dato dall'utente * 5.determinare il numero di libri disponibili per il prestito; */ import java.io.*; public class main1 { public static void main (String args[]) throws IOException{ book [] list = new book [500]; //valore indicativo list [00] = new book ("Sturm j.", "Sviluppo di soluzioni XML", "Mondadori informatica", 2000, "P002", "15/12/2002", true); list [01] = new book ("Eco U.", "Il nome della rosa", "La biblioteca di Repubblica", 2002, "p003", "17/01/2003", false); list [02] = new book ("Vieveg M.", "Ragazze di Boemia", " Arnoldo Mondadori", 1999, "P001","08/10/2002", true); list [03] = new book ("Camilleri A.","La forma dell'acqua","Sellerio",1998, null, null, false ); list [04] = new book ("Redmond P.", "l'allievo","arnoldo mondadori", 2001, "P004", "28/03/2003", false); char choice; String str; int temp = 0; int n = 0; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out,true); do{ book.menu(); choice = (char) System.in.read(); if(choice == 'q')break; switch (choice){ case '1': n=n+1; book.Insert("Autore"); str = br.readLine(); book.auth(list, str); book.Insert("Titolo"); str= br.readLine(); book.tit(list, str); book.Insert("Editore"); str= br.readLine(); book.edit(list, str); book.Insert("Anno Publicazione"); try{ str= br.readLine(); temp = Integer.parseInt(str); }catch(NumberFormatException exc){ System.out.println("il dato inserito non è corretto"); } book.year(list, temp); book.Insert("Id Prestito"); str=br.readLine(); book.idLoan(list, str); book.Insert("Presenza registrata"); book.presence(list, true); book.repilog(list); break; case '2': book.list(list); break; case '3': str = br.readLine(); book.compare(list, str); break; case '4': //non ho ancora risolto questo punto, Consigli? break; case '5': book.number(list, temp); break; } }while(choice =='\r'|| choice=='\n' && choice != 'q'); } }


Rispondi quotando
