簡體   English   中英

需要幫助找出發生此錯誤的原因

[英]Need help finding out why this error occurs

我的代碼是這樣的:

 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值

public void main(String[ ] args){

應該

public static void main(String[ ] args){

如果沒有適當的main -method聲明,則JVM沒有入口點。

話雖如此,看起來當前“主”中的代碼確實應該在您的類的構造函數中-看起來您可能打算在main -方法中創建類的實例。

您的main方法當前不作為輸入方法。 應該定義為static

  public static void main(String[ ] args){

我假設這與您的IDE有關。 具體來說,它正在尋找main()非靜態版本。

這個:

public void main(String[ ] args){

}  

實際上應該是:

public static void main(String[ ] args){

}  

...當然,這意味着this引用不再起作用-您需要首先實際創建chromeNPlayerScreen

public static void main(String[ ] args){
   chromeNPlayerScreen screen = new chromeNPlayerScreen();
   screen.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
   screen.add(dPnl);
   screen.setSize(600,600);;
   screen.setVisible(true);
   screen.setResizable(false);
   screen.setLocation(200, 200);
}  

您的主體不是靜態的,請替換為:

public void main(String[] args) 

這樣 :

public static void main(String[] args)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM