通常の配列からオブジェクトを取得しようとすると、null ポインター例外が発生しました。まず、10 個のLine
オブジェクトで配列を初期化し、配列内の各オブジェクトの値を設定します。しかし、オブジェクトの値を取得すると、それが 0 であることがわかりました。なぜそれが起こるのですか?
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class LineArrayLimt extends Applet
implements MouseListener, MouseMotionListener {
int width, height;
int x, y, lineCounter; // the coordinates of the upper-left corner of the box
int mx, my; // the most recently recorded mouse coordinates
int new_mx;
int new_my;
Thread th;
boolean isMouseDragging = false, isStored;
Line[] lines = new Line[10];
class Line {
Line() {
}
Line(int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
private int x1, y1, x2, y2;
public void setX1(int x) {
this.x1 = x;
}
public void setY1(int y) {
this.y1 = y;
}
public void setX2(int x) {
this.x2 = x;
}
public void setY2(int y) {
this.y2 = y;
}
public int getX1() {
return x1;
}
public int getY1() {
return y1;
}
public int getX2() {
return x2;
}
public int getY2() {
return y2;
}
}
public void init() {
width = getSize().width;
height = getSize().height;
setBackground(Color.black);
addMouseListener(this);
addMouseMotionListener(this);
for (int i = 0; i < lines.length; i++) {
lines[i] = new Line();
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
System.out.println("-----------------Mouse Pressed------------------------");
mx = e.getX();
my = e.getY();
}
public void mouseReleased(MouseEvent e) {
System.out.println("-----------------Mouse Released------------------------");
if (isMouseDragging) {
if (lineCounter != lines.length - 1) {
lines[lineCounter].setX1(mx);
lines[lineCounter].setY1(my);
lines[lineCounter].setX2(new_mx);
lines[lineCounter].setY2(new_my);
lineCounter++;
if (lines[lineCounter] != null) {
System.out.println("-----------------First Object------------------------" + lines[lineCounter].getX1() + " " + lines[lineCounter].getY1() + " " + lines[lineCounter].getX2() + " " + lines[lineCounter].getY1());
}
}
}
isMouseDragging = false;
}
public void mouseMoved(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
System.out.println("-----------------Mouse Dragged------------------------");
isMouseDragging = true;
new_mx = e.getX();
new_my = e.getY();
if (new_mx <= 35) {
new_mx = 35;
}
if (new_mx > width - 40) {
new_mx = width - 40;
}
if (new_my > height - 40) {
new_my = height - 40;
}
if (new_my < 35) {
new_my = 35;
}
repaint();
}
public void paint(Graphics g) {
System.out.println("-----------------Line No." + lineCounter + " is inside paint------------------------");
g.setColor(Color.RED);
if (isMouseDragging) {
System.out.println("-----------------Paint while dragging------------------------");
g.drawLine(mx, my, new_mx, new_my);
if (lineCounter != lines.length - 1) {
//if(lines[lineCounter]!=null){
g.drawLine(lines[lineCounter].getX1(), lines[lineCounter].getY1(), lines[lineCounter].getX2(), lines[lineCounter].getX2());
System.out.println("*************" + lines[lineCounter].getX1() + lines[lineCounter].getY1() + lines[lineCounter].getX2() + lines[lineCounter].getX2());
//}
}
}
}
}