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

标签 java nullpointerexception applet

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

我的小程序代码是

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中尝试了这段代码,它运行完美,但是当我调整窗口大小时,我给出了相同的错误。

请帮忙。

最佳答案

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

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

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

关于java - java Applet 中线程 "AWT-EventQueue-1"java.lang.NullPointerException 中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28795284/

相关文章:

JavaFX:嵌入式 JavaFX 小程序丢失宽度和高度?

java - 结合 BigQuery 和 Pub/Sub Apache Beam

java Callable FutureTask Excecuter : How to listen to finished task

java - JavaFX 应用程序线程中按 ENTER 键时出现 NullPointerException

java - 使用给定证书在 JWS 应用程序中设置发布者名称

authentication - 从服务器自动访问 USB 存储中的文本文件内容

java - JMS:MessageListener的onMessage中消息可以为空吗?

java - 在tomcat上获取变量的描述

java - 为什么在将数据从 Activity 传递到 Fragment 时会出现空指针异常?

java - 在一个类中使用另一个类中的变量 NullpointerException - RunTimeException