nullpointerexception - Java Null Pointer Exception when creating new instance of class -
I am in the first year of my computer science and I am having a hell of time with a project. I was given the code which lists the number of rooms through an array, and I am preparing a method that allows the user to choose a room, enter his information and reserve the room.
I lie in making this issue that actually protects the room ... which is as follows ...
Public Zero Booker (Cell [] Room, int nooforms) {// Get settings for the room (smoking, occupied, guest's name / number) displayrooms.info (rooms, numbersoforms); Room Room 1 = New Room (false, false, empty, null, numberoffrooms); // User Input, store each value in the temporary variable Scanner scan = new Scanner (System.in); System.out.println ("Please choose a room"); Int guestRoomNum = scan.nextInt (); room1.setRoomNumber (guestRoomNum); // set room [i] captured = true; Room1.setOccupied (true); // set room [i] guestname name system.out.println ("enter guest name"); String Dictit = Scan. Nxt (); String [] nameList = {dickshit}; Room1.setGuestName (nameList); // set room [i] guest.number to phone System.out.println ("Enter guest phone number"); String Phone Input = Scan. Nxt (); Room1.setGuestPhone (phoneInput); } I'm getting an empty indication exception on the cell <1> new cell (false, false, empty, empty, orphosum);
This is the line where I should actually create an example of a custom class that was provided to me.
The cell class code is as follows:
Public square room {Private boolean smoking; Private boolean capture; Private string [] guestName = new string [4]; Private string guestphone; Private int room number; Public room (boolean smoking, boolean occupation, string [] guest name, string guest phone, int room number) {this.smoking = smoking; This.occupied = Captured; For (Int i = 0; I ++) this .guestName [i] = guestName [i]; This.guestPhone = guestPhone; This.roomNumber = roomNumber; } Public Zero setGuestName (string [] guestName) {for (int i = 0; i <4; i ++) this.guestName [i] = guestName [i]; } Public string [] getGuestName () {string [] tempGuestName = new string [4]; TempGuestName for (int i = 0; i }
This is because your in the cell () constructor, you are doing this. For (int i = 0; i <4 ; I ++) This .guestName [i] = guestName [i]; But for the guestName as the parameter null , which is your guestName array zero . If you want to pass a zero So remove the look from the constructor. If not, pass the constructor blank string [] . room cell 1 = new room (false, false, new string [4], empty, numberoffooms); Note that whenever you specify above, each element in string [] is still a null reference and you It is necessary to start them explicitly later in their program.
Comments
Post a Comment