java - NullPointException error in Book object program -
For my life I could not find this logical error in my code ... the error says:
I have a code that comments the part of the larger program that I am attempting to create, but do not pay attention to this issue for now.
Exception in the thread "main" on java.lang.NullPointerException SchoolTextBookSort.main (SchoolTextBookSort.java18)
And here is my code and thank you in advance !:
import java.util.Arrays; Import javax.swing *; Public class school textbook {public stationary zero main (string [] Args) // Tudo auto-generated method stub //javax.swing.SwingUtilities.invokeLater (new runall) (// public zero run () {// CreateAndShowGUI ();}}}; School Textbook [] theBooks = New School Textbook [5]; JoppenPan. Show Message Dialeg (empty, books); The book [0] .setAuthor ("Ernest Hemingway"); The Book [1] .setAuthor ("Mark Twain"); The Book [2] .setAuthor ("William Shakespeare"); The Book [3] .setAuthor ("St The Book [4] .setAuthor ("William Falconer"); The Book [0] .setTitle ("A Farewell to Palms"); The Book [1] .setTitle ("The Adventures of Hucklebury Finn" ); TheBooks [2] .setTitle ("Hamlet"); The Book [3] .setTitle ("Lot of Salem"); The Book [4] .setTitle ("Sound and Fury"); theBooks [0] .setPageCount (332); theBooks [1] .setPageCount (320); theBooks [2] .setPageCount (196); theBooks [3] .setPageCount (439); theBooks [4] .setPageCount (326); theBooks [0] .setISBN (0099910101D); theBooks [1] .setISBN (+0142437174); TheBooks [2] .setISBN (0521618746D); TheBooks [3] .setISBN (+0450031063); TheBooks [4] .setISBN (0679732241D); TheBooks [0] .setPrice (5.99); TheBooks [1] .setPrice (7.60); TheBooks [2] .setPrice (9.41); TheBooks [3] .setPrice (16.56); TheBooks [4] .setPrice (9.60); Employmentpan Show Message Dialeg (blank, books); Arrays.sort (Books, SchoolTextBook.BookAuthorComparator); // for (Int i = 0; I & lt; theBooks.length; i ++) {//}} Make Private Static Zero and ShowOvi () {// Create and Set Window JFrame Frame = New JFram (" Book sorting "); Frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); // Create content pane and set JComponent newContentPane = new radiobootname (); NewContentPane.setOpaque (true); // content pens should be opaque frame .set contentpane (new containpan); // window display frame.pack (); Frame.setVisible (true); }} And here is the school textbook class code:
import java.util.Comparator; Public class SchoolTextBook {Private String Writer; Private string title; Private intimate page count; Private Double ISBN; Private double price; Public string getAuthor () {return author; } Public Zero Set Writer (string author) {this.author = author; } Public string getTitle () {return title; } Public Zero Sattitles (String title) {this.title = title; } Public int getPageCount () {return page count; } Public Zero Set Page Count (Int. PageCount) {this.pageCount = pageCount; } Public double getISBN () {return ISBN; } Public Zero Set ISBN (Double ISBN) {ISBN = ISBN; } Public double getprice () {return value; } Public Zero Set Processes (Double Pricing) {this.price = price; } Public stable comparative & lt; SchoolTextBook & gt; BookAuthorComparator = New Comparison & lt; SchoolTextBook & gt; () {Compare Public Entry (School Textbook Book 1, School Textbook Book 2) {String Book Name 1 = Book 1. Gate Officer () ToUpperCase (); String book name 2 = book2.getAuthor (). ToUpperCase (); // ascending order return bookback 1 compter to (booknames 2); }}; Public stable comparative & lt; SchoolTextBook & gt; BookTitleComparator = New Comparison & lt; SchoolTextBook & gt; () {Compare public int (school text book book 1, school text book 2 book) {string bookname 1 = book 1.getTitle (). ToUpperCase (); String Book Name 2 = book2.getTitle (). ToUpperCase (); // ascending order return bookback 1 compter to (booknames 2); }};
You can start your code at any time with the SchoolTextBook examples Do not you need to populate each location by calling the school textbook the constructor for the first time. Your code is now:
schoolbook [] thebooks = new SchoolTextBook [5]; ... books [0] .setAuthor ("Ernest Hemingway"); ... more than one ... The first line only starts the array; It does not populate it with the school textbook examples (each location is null ). You have to do this instead: school textbook [] theBooks = new SchoolTextBook [5]; ... books [0] = new school textbook (); The Book [0] .setAuthor ("Ernest Hemingway"); ... and so on ...
Comments
Post a Comment