开发者

Java Error to do with array searching

Hey guys i'm trying to use drawString() function to draw the result from a search in a array. I am using the code below

import java.awt.Graphics;

public class canvas extends JPanel{
    int i, count;
    public String read_string = "";
    public String[] names = {"Duncan","Matthew","Kevin","Etc"};
    public String[] searchfor = {"Duncan","Kevin"};
    public canvas() {

    search();


    }

    public void search() {

        for(i=0; i<names.length; i++) {
            read_string = names[i];
            if(read_string.contains("Duncan") || read_string.contains("Kevin")) {

                count++;

                System.out.println(read_string);
                drawThatText(null, read_string, 500*i + 1, 500*i + 1);
            } else {

            }


        }
    }


    public void drawThatText(Graphics g, String s, int x, int y) {


 开发者_Go百科       g.drawString(s, x, y);

    }
}

I get the following error

Duncan
java.lang.ExceptionInInitializerError
Caused by: java.lang.ArithmeticException: / by zero
    at canvas.search(canvas.java:33)
    at canvas.<init>(canvas.java:19)
    at Client.<clinit>(Client.java:10)
Exception in thread "main" 


drawThatText(read_string, 500*i + 1, 500*i + 1); is line 33

That line can't possibly throw a division by zero exception.

I do see however that you could get a NullPointerException since you call

drawThatText(null, ...

and then do

public void drawThatText(Graphics g, String s, int x, int y) {
                         ^^^^^^^^^^^
                       will equal null

    g.drawString(s, x, y);
    ^
    |
    '--- Will throw a NullPointerException

}

Here's a different version of the program which should give you a push in the right direction:

import java.awt.Graphics;
import javax.swing.*;
public class canvas extends JPanel {
    int i, count;
    public String read_string = "";
    public String[] names = {"Duncan","Matthew","Kevin","Etc"};
    public String[] searchfor = {"Duncan","Kevin"};

    @Override
    public void paintComponent(Graphics g) {
        for(i=0; i<names.length; i++) {
            read_string = names[i];
            if(read_string.contains("Duncan") ||
               read_string.contains("Kevin")) {
                count++;
                System.out.println(read_string);
                drawThatText(g, read_string, 50*i + 10, 50*i + 10);
            }
        }
    }

    public void drawThatText(Graphics g, String s, int x, int y) {
        g.drawString(s, x, y);
    }

    public static void main(String[] args) {
        JFrame jf = new JFrame();
        jf.setContentPane(new canvas());
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setSize(200, 200);
        jf.setVisible(true);
    }
}

Java Error to do with array searching


You are passing a null Graphic object to drawThatText().

EDIT:

you should eventually retrieve the Graphic object from the Component that you want to be drawn. Use this method:

yourComponent.getGraphics(); //where yourComponent is the component you want do draw on.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜