【问题标题】:Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException in java Appletjava Applet 中的线程“AWT-EventQueue-1”java.lang.NullPointerException 中的异常
【发布时间】:2015-03-02 01:10:56
【问题描述】:

我正在用 java 创建一个程序,它从 applet 读取文本文件并将颜色放入由文件定义的 applet 窗口像素中。 问题是当我运行这个程序时发生异常,我已经尽我所能解决它但没有成功。

我的小程序代码是

GUIIO.java

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class GUIIO extends Applet {
Color color = new Color(2);
InputStream inputStream;
BufferedReader bufferedReader;

@Override
public void init() {
    try {
        inputStream = new FileInputStream("Sample In.txt");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public void paint(Graphics g) {
    Point point = new Point();
    try {
        bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        point = this.getImageResolution(bufferedReader);
        char c, ch[] = {'a', 'b', 'c', 'd', 'e', 'f' };
        int i = 0, x = 0, y = 0;
        putPixel(1, 100, String.valueOf(ch), g);
        while((c = (char)bufferedReader.read()) != 'z') {
            if(c == 'y') {  
                y++;
                x = 0;
            }
            else if(i < 6) {
                ch[i] = c;
                i++;
            }
            if (i == 6) {
                putPixel(x, y, String.valueOf(ch), g);
                x++;
                i = 0;
                ch[i] = c;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    g.drawString(point.x+" "+point.y, 30, 10);
    repaint();
}

private Point getImageResolution(BufferedReader bufferedReader) throws IOException {
    boolean xFlag = false;
    StringBuilder x = new StringBuilder(), y = new StringBuilder();
    String check = null;
    char chars[] = bufferedReader.readLine().toCharArray();
    for(int i=0;i<chars.length;i++) {
        check = String.valueOf(chars[i]);
        if(check.equals("x"))   xFlag = true;
        else if(xFlag == true)  y.append(check);
        else    x.append(check);
    }
    Point point = new Point();
    point.x = Integer.parseInt(x.toString());
    point.y = Integer.parseInt(y.toString());
    return point;
}

private void putPixel(int x, int y, String color, Graphics g) {
    g.setColor(Color.decode("0x"+color));
    g.drawLine(x, y, x, y);
}
}

输入文本文件。

示例 In.txt

8x8
000000ff000000ff000000ff000000ff000000ff000000ffy000000ff000000ff000000ff000000ff000000ff000000ffy000000ff000000ff000000ff000000ff000000ff000000ffy000000ff000000ff000000ff000000ff000000ff000000ffy000000ff000000ff000000ff000000ff000000ff000000ffy000000ff000000ff000000ff000000ff000000ff000000ffy000000ff000000ff000000ff000000ff000000ff000000ffy000000ff000000ff000000ff000000ff000000ff000000ffyz

我在eclipse中遇到的异常或错误

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at GUIIO.getImageResolution(GUIIO.java:61)
at GUIIO.paint(GUIIO.java:30)
at sun.awt.RepaintArea.paintComponent(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

更新:我在 cmd 中尝试过这段代码,它运行得很好,但是当我调整窗口大小时,我给出了同样的错误。

请帮忙。

【问题讨论】:

  • 那么,第 61 行是什么?为什么要从小程序中的文件中读取?为什么每次重新绘制小程序时都从文件中读取?
  • 1) 为什么要编写小程序?如果是老师指定的,请参考Why CS teachers should stop teaching Java applets。 2) 为什么使用 AWT?请参阅this answer 了解放弃 AWT 使用支持 Swing 的组件的许多充分理由。

标签: java nullpointerexception applet


【解决方案1】:

您的代码中有多个问题。 首先,您无法从 bufferedReader 读取和读取,因为有时它会为空,并且您不会检查从 bufferedReader 读取的行是否为空。

还有一个问题,有时候你的颜色字符串是?????? .

为什么每次重绘 aplet 时都要读取文件? 您可以在 init() 方法中读取文件一次,就是这样!

【讨论】:

  • 谢谢兄弟,我明白你所说的,代码现在可以工作了。
猜你喜欢
  • 2011-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-21
  • 2013-12-09
  • 1970-01-01
相关资源
最近更新 更多