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) {
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"
That line can’t possibly throw a division by zero exception.
I do see however that you could get a
NullPointerExceptionsince you calland then do
Here’s a different version of the program which should give you a push in the right direction: