1

I am creating a program in java which reads a text file from applet and puts colors in applet window pixels defined by file. The problem is that when i run this program the Exception occurs and i have done everything i know to resolve it but didn't succeed.

My applet code is

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);
}
}

The input text file.

Sample In.txt

8x8
000000ff000000ff000000ff000000ff000000ff000000ffy000000ff000000ff000000ff000000ff000000ff000000ffy000000ff000000ff000000ff000000ff000000ff000000ffy000000ff000000ff000000ff000000ff000000ff000000ffy000000ff000000ff000000ff000000ff000000ff000000ffy000000ff000000ff000000ff000000ff000000ff000000ffy000000ff000000ff000000ff000000ff000000ff000000ffy000000ff000000ff000000ff000000ff000000ff000000ffyz

The exception or error i have got in 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)

Update: I have tried this code in cmd it runs perfectly but when i resized the window i gives same error.

Please help.

  • So, what is line 61? Why do you read from a file in an applet? Why do you read from a file every time the applet is repainted? – JB Nizet Mar 01 '15 at 16:11
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Mar 02 '15 at 02:07

1 Answers1

0

There are muliple issues in your Code. First you can't read and read and read from your bufferedReader because at sometime it will be empty and you don't check if the line that you read from the bufferedReader is null.

There is also another problem, sometimes your color String is ?????? .

Why do you read the file every time the apllet is repainted? You could read the file once in your init() methode and that's it!

intrigus
  • 206
  • 1
  • 7