你的错误贴的完全没法看,我把你的代码复制过来运行了下,错误信息应该复制这些:
我们一直强调成都网站建设、成都网站设计对于企业的重要性,如果您也觉得重要,那么就需要我们慎重对待,选择一个安全靠谱的网站建设公司,企业网站我们建议是要么不做,要么就做好,让网站能真正成为企业发展过程中的有力推手。专业的建站公司不一定是大公司,创新互联作为专业的网络公司选择我们就是放心。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at testquestion.Ran$1.actionPerformed(Ran.java:41)
很明显是空指针错误
再看41行:
if(bool[number] == true){
就是说bool[number]这时的值是null
我看了下前面的定义:
private static Boolean[] bool;
然后在后面定义了
bool=new Boolean[52];
需要注意的是boolean和Boolean的区别,boolean是基本类型,不需要去new创建,而Boolean虽然用法基本一样,但他是个对象,你光创建了一个Boolean对象数组但没有一一去创建对象,所以bool[number]取得的仍然是null
还需要进行的一步是
for(int i=0;ibool.length;i++){
bool[i]=false;
}
对于你代码里的
if(bool[number] == true){
value = number;
bool[number] = true;
}
比较费解如果bool[number]==true已经成立再bool[number]=true实在有些多此一举,可能你想写的是false,我先自作主张帮你改成这个了.
修改完后再运行,成功!
将 deliverCard();//发牌
repaint();
修改为
new Thread()
{
public void run()
{
deliverCard();//发牌
repaint();
}
}
.start();
不过你的dowhile是个死循环 症结在这里
帮你运行:
F:\oopjavac Test05.java
F:\oopjava Test05
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1090)
at java.awt.Container.add(Container.java:410)
at Test05.init(Test05.java:23)
at Test05.main(Test05.java:34)
错误在line:23行,注释掉后:
this.setLayout(new GridLayout(3,1));
mb1.add(bq1);mb1.add(wbk);
mb2.add(bq2);mb2.add(mmk);
//mb3.add(an1);mb3.add(an2);
this.add(mb1);
this.getContentPane().add(mb2);
//this.getContentPane().add(mb3);
this.setTitle("登陆窗体");
然后就发现对了,界面出来了。然后看23行:
mb3.add(an1); mb3.add(an2);
换行后执行,返现错的是24行:
mb3.add(an1);
mb3.add(an2);
仔细一看,亲,你的an2有没有初始化啊!!!!
an1=new JButton("登陆");
an1=new JButton("退出");
看看上面的代码。。。。。。。。。。我能咋样。好好的练习吧,还是要多写代码,多改错。
执行如下代码后报错信息是:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 6 out of bounds for length 6
at SortArray.main(SortArray.java:21)
package shiyan3;
import java.io.IOException;
public class SortArray {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int m;
// TODO Auto-generated method stub
int n;
// TODO Auto-generated method stub
int k;
int[] aa = new int[6];
int[] bb = new int[6];
int[] cc = new int[12];
for (int i = 0; i = 6; i++) {
m = (int) (100 * Math.random());
aa[i] = m;
n = (int) (100 * Math.random());
bb[i] = n;
System.out.println(aa[i] + " " + bb[i]);
}
for (int i = 0; i 6; i++) {
for (int j = i; j 6; j++) {
if (aa[i] aa[j]) {
int t = aa[i];
aa[i] = aa[j];
aa[j] = t;
}
if (bb[i] bb[j]) {
int t = bb[i];
bb[i] = bb[j];
bb[j] = t;
}
}
}
m = 0;
n = 0;
k = 0;
while ((m == 6) (n == 6)) {
if (aa[m] = bb[n]) {
cc[k] = aa[m];
m++;
} else {
cc[k] = bb[n];
n++;
}
k++;
}
while (m == 6) {
cc[k] = aa[m];
m++;
k++;
}
while (n == 6) {
cc[k] = bb[n];
n++;
k++;
}
for (int i = 0; i 12; i++) {
System.out.print(cc[i] + " ");
}
}
}
第21行是从0到6,也就是7次循环,应该把=6改为6。
F:\oopjavac Test05.java
F:\oopjava Test05
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1090)
at java.awt.Container.add(Container.java:410)
at Test05.init(Test05.java:23)
at Test05.main(Test05.java:34)
错误在line:23行,注释掉后:
this.setLayout(new GridLayout(3,1));
mb1.add(bq1);mb1.add(wbk);
mb2.add(bq2);mb2.add(mmk);
//mb3.add(an1);mb3.add(an2);
this.add(mb1);
this.getContentPane().add(mb2);
//this.getContentPane().add(mb3);
this.setTitle("登陆窗体");
然后就发现对了,界面出来了。然后看23行:
mb3.add(an1); mb3.add(an2);
换行后执行,返现错的是24行:
mb3.add(an1);
mb3.add(an2);
仔细一看,亲,你的an2有没有初始化啊!!!!
an1=new JButton("登陆");
an1=new JButton("退出");
看看上面的代码。。。。。。。。。。我能咋样。好好的练习吧,还是要多写代码,多改错。
分类: 电脑/网络 程序设计 其他编程语言
问题描述:
本程序要实现的是随着鼠标点击,将在画布上话小方块。
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.geom.*;
import javax.swing.*;
public class MouseTest
{
public static void main(String[] args)
{
MouseFrame frame=new MouseFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class MouseFrame extends JFrame
{
public MouseFrame()
{
setTitle("MouseTest");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
MousePanel panel=new MousePanel();
Container contentPane=getContentPane();
contentPane.add(panel);
}
public static final int DEFAULT_WIDTH=300;
public static final int DEFAULT_HEIGHT=200;
}
class MousePanel extends JPanel
{
public MousePanel()
{
squares=new ArrayList();
current=null;
addMouseListener(new MouseHandler());
addMouseMotionListener(new MouseMotionHandler());
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
g2.setPaint(Color.RED);
for(int i=0;isquares.size();i++)
g2.draw((Rectangle2D)squares.get(i));
}
public Rectangle2D find(Point2D p)
{
for(int i=0;isquares.size();i++)
{
Rectangle2D r=(Rectangle2D)squares.get(i);
if(r.contains(p)) return r;
}
return null;
}
public void add(Point2D p)
{
double x=p.getX();
double y=p.getY();
current=new Rectangle2D.Double(x-SIDELENGTH/2,y-SIDELENGTH/2,SIDELENGTH,SIDELENGTH);
squares.add(current);
repaint();
}
public void remove(Rectangle2D s)
{
if(s==null)return;
if(s==current) current=null;
squares.remove(s);
repaint();
}
private static final int SIDELENGTH=10;
private ArrayList squares;
private Rectangle2D current;
private class MouseHandler extends MouseAdapter
{
public void mouseHandler(MouseEvent event)
{
current=find(event.getPoint());
if(current==null)
add(event.getPoint());
}
public void mouseClicked(MouseEvent event)
{
current=find(event.getPoint());
if(current==nullevent.getClickCount()=2)
remove(current);
}
}
private class MouseMotionHandler implements MouseMotionListener
{
public void mouseMoved(MouseEvent event)
{
if(find(event.getPoint())==null)
setCursor(Cursor.getDefaultCursor());
else
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
}
public void mouseDragged(MouseEvent event)
{
if (current!=null)
{
int x=event.getX();
int y=event.getY();
current.setFrame(x-SIDELENGTH/2,y-SIDELENGTH/2,SIDELENGTH,SIDELENGTH);
repaint();
}
}
}
}
解析:
这地方是改错的,不是给你实现功能的,哈哈