线程中的异常" AWT-EventQueue-1" java Applet中的java.lang.NullPointerException

时间:2015-03-01 15:24:39

标签: java nullpointerexception applet

我在java中创建一个程序,它从applet读取文本文件,并将颜色放在由文件定义的applet窗口像素中。  问题是,当我运行这个程序时,Exception发生了,我已经做了我知道的所有事情来解决它但没有成功。

我的小程序代码是

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中尝试过这个代码,它运行得很好,但是当我调整窗口大小时,我也给出了同样的错误。

请帮忙。

1 个答案:

答案 0 :(得分:0)

您的代码中存在多个问题。 首先,您无法读取和读取和读取您的bufferedReader,因为在某些时候它将为空,您不会检查从bufferedReader读取的行是否为空。

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

为什么每次重新绘制apllet时都会读取文件? 你可以在init()方法中读取一次文件,那就是它!