问题
I'm currently attempting to create a tic-tac-toe game in java as a college project. So far all I've done is created the skeleton code of a gameBoard method. I'm already running into an error saying :
java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
I'm also unsure on how to accomplish the project in general. My basic idea is to use the gameBoard method to print the board every turn. I'm attempting to make the variables global so that I can alter them when a player makes a move. How do I accomplish this, and how do I fix the error I'm getting? Any help would be greatly appreciated, here is my current code:
import javax.swing.JOptionPane;
public class Assignment5
{
public static String top1 = " ";
public static String top2 = " ";
public static String top3 = " ";
public static String mid1 = " ";
public static String mid2 = " ";
public static String mid3 = " ";
public static String bot1 = " ";
public static String bot2 = " ";
public static String bot3 = " ";
//Generate the game board
public static void gameBoard()
{
System.out.println("+-----------+");
System.out.println("| " + top1 + " | " + top2 + " | " + top3 + " |");
System.out.println("+-----------+");
System.out.println("| " + mid1 + " | " + mid2 + " | " + mid3 + " |");
System.out.println("+-----------+");
System.out.println("| " + bot1 + " | " + bot2 + " | " + bot3 + " |");
System.out.println("+-----------+");
}
public void main(String args[])
{
gameBoard();
}
}
回答1:
Regarding null pointer exception, it is the exception That isthrown when an application attempts to use null in a case where an object is required.
Here is the corresponding class in java: java.lang.NullPointerException
Regarding your code I would suggest you to use IDE like Eclipse which can you point you the compilation errors as you type like in below case
public void main(String args[])
It should be
public static void main(String args[])
回答2:
You've got a NPE
because your class doesn't have a main()
, with a static
keyword in it. It seems that the you've used the run
command instead of the java
command to execute your code(using the DrJava's java compiler).
There is a Bug Report which was filed on the SourceForge page of DrJava, which shows a misleading stacktrace
whenever run
is used instead of java
, for cases, where the static
keyword is not given in the main()
.
So how to solve this problem?
You need to provide the proper signature of the main()
method, which is as below.
public static void main(String args[]) // static keyword required
回答3:
You may want to represent your game board with an array of arrays, such as
String[][] board = {new String[3], new String[3], new String[3]};
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[i].length; j++) {
board[i][j] = " ";
}
}
This way you can print out the board's contents with a nested loop rather than having to reference each element separately; you can also determine board element adjacency more easily (e.g. you can determine if somebody has won using a loop rather than using a hellish if-elseif or switch statement).
Regarding your null pointer exception, nothing in the code you've posted should be causing one; the error must lie in the rest of your code.
回答4:
When you create a variable to hold an object, you are actually creating a reference which then must be assigned to an object instance of the declared type (or subtype).
For example
Car c = new Car();
The variable c
is a reference, which is assigned to point to the newly created object new Car()
.
If you just said this on the other hand:
Car c;
Now you have a reference that does not point to anything. Since it does not point to anything, it points to null, in other words it is a null pointer. null is an abstraction that indicates nothingness. So if you now tried to use that null pointer as if it contained something:
c.drive();
You would now get a NullPointerException. It means you tried to invoke a method on a reference that doesn't actually point to anything.
来源:https://stackoverflow.com/questions/16330231/what-is-a-nullpointerexception