我的代碼是這樣的:需要幫助找出爲什麼會出現此錯誤
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Font;
import java.awt.Graphics;
import java.net.URL;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.*;
import javax.swing.Timer;
public class chromeNPlayerScreen extends JFrame implements ActionListener{
DrawScreen dPnl = new DrawScreen();
public void actionPerformed(ActionEvent e){
}
public void main(String[ ] args){
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.add(dPnl);
this.setSize(600,600);;
this.setVisible(true);
this.setResizable(false);
this.setLocation(200, 200);
}
}
但是當我運行它.....
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)
有人能向我解釋爲什麼這個心不是工作?
的DrawScreen代碼
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Font;
import java.awt.Graphics;
import java.net.URL;
public class DrawScreen extends JPanel {
String picPath = "pictures/";
ClassLoader cl = pokemonChromeNewPlayerScreen.class.getClassLoader();
URL imgURL = cl.getResource(picPath+"welcomeBG.png"),imgURL2 = cl.getResource(picPath+"dialogBox.png"),
imgURL3 = cl.getResource(picPath+"Professor.png");
Toolkit tk = Toolkit.getDefaultToolkit();
Image imgBG, imgDialog, imgProfessor;
public void imgImport(){
imgBG = tk.createImage(imgURL);
imgDialog = tk.createImage(imgURL2);
imgProfessor = tk.createImage(imgURL3);
}
public void paintComponent(Graphics g) {
g.setColor(Color.BLACK);
Graphics2D g2 = (Graphics2D)g;
for(int x=0;x<=600;x+=25){
g2.drawLine(x,0,x,600);
g2.drawString(""+x,x+5,20);
}
for(int y=0;y<=600;y+=25){
g2.drawLine(0,y,600,y);
g2.drawString(" "+y,0,y+20);
}
}
}
這是DrawScreen代碼,ATM所做的只是將一個網格,但是那是因爲我剛開始它,並希望在x,y值不同的位置
爲什麼不呢? – arshajii
我的一位朋友告訴我,當在java中調用main()時,該類從未實例化過,因此任何類成員都將爲null。而且由於你不在某個對象的特定實例中,因此不存在'this'。 –
你如何運行你的程序? –