Reputation: 107
Don't want any code, just want some sort of guidance. Would like to keep my academic integrity in tact ;)
I keep getting that annoying error. I need to call the toString method for each Room instance. Any suggestions? I would prefer an answer within 2 hours if at all possible.
public class Hotel
{
//constant
public static final int NUM_ROOMS = 20;
//variables
public Room[] theRoom;
public String name;
public int totalDays;
public double totalRate;
public int singleCount;
public int doubleCount;
public int roomsRented;
public int NOT_FOUND;
public Hotel(String newName) {
name = newName;
Room[] Rooms = new Room[NUM_ROOMS];
}
public double getTotalRentalSales() {
return totalRate + roomsRented;
}
public double getAvgDays() {
return roomsRented/totalDays;
}
public double getAvgRate() {
return totalRate/roomsRented;
}
public int getSingleCount() {
return singleCount;
}
public int getDoubleCount() {
return doubleCount;
}
public String printRentalList() {
System.out.println("Room Information: " + Room.toString());
}
}
Upvotes: 8
Views: 21906
Reputation: 51
When you are calling classname.method(), it means that you are calling a static method. We use the classname inorder to call the static methods.
Here class is : Room
Method is : toString()
toString() is not a static method to be called with class name.
toString() is the method of the Object class.
So you should use room.toString() where 'room' is the object of the Room class
Upvotes: 0
Reputation: 11
System.out.println(Arrays.toString(arr));
In this line when you are writing arrays a dropdown menu will appear, there you will see the default package or the java.util
package. You need to select the java.util
package and the problem will be solved.
Upvotes: 1
Reputation: 11
Look at the following code, you can compile toString with a static variable without a new object, it just throw exception at run time
demo>cat Test.java
class Water {
public String toString() {return "whatever";}
}
public class Test {
static Water water;
public static void main(String...args) {
System.out.println(water.toString());
}
}
demo>javac Test.java
demo>java Test
Exception in thread "main" java.lang.NullPointerException
at Test.main(Test.java:8)
Upvotes: 1
Reputation: 285405
You shouldn't try to call toString()
on a Room class but rather on a Room object. In that method, loop through the array of rooms with a for loop and print the String returned by calling toString()
for each Room object held in the array since this is what it looks like your method should do.
For example
System.out.println("All Foos held here include: ");
// using a "for-each" loop, assuming an array called fooArray that holds Foo objects
for (Foo foo: fooArray) {
System.out.println(foo);
}
You will obviously have to change the types and variable names for your code.
Edit 2: although you will have to use a standard for loop, not a for-each loop, since you won't be looping through the entire array, but rather will quit when roomsRented count is reached.
System.out.println("All Foos held here include: ");
// using standard for loop, assuming an array called fooArray that holds Foo objects
for (int i = 0; i < someMaxNumber; i++) {
System.out.println(fooArray[i]);
}
Upvotes: 6
Reputation: 33
What you're probably doing is calling toString()
on the class Room, not an instance of it. For example, instead of writing:
Room.toString()
write:
Room r = new Room()
r.toString()
Upvotes: 1
Reputation: 3279
As error is already states, do not call instance method in static context.
Room is a class, not an object. toString is a instance method. So Room.toString() in this case compiler looks for a static method toString. But toString is an instance method so it is causing an issue.
Always remember instance methods are called with the object of the class, not with class itself.
Upvotes: 1