我的程序中Exception in thread "main" java.lang.NullPointerException怎么解决

junzhangdemingzi 2009-04-09 03:00:18
以下是我写的一个简单的程序,编译时没问题,运行时却抛出异常
Exception in thread "main" java.lang.NullPointerException
at home1.<init>(home1.java:16)
at home1.main(home1.java:41)

这是我的程序
import java.awt.*;
//import java.awt.event.*;
import javax.swing.*;
public class home1 extends JFrame
{private JButton buttons[],submit;
private JLabel labels[];
private JTextField fields[];
private String[] strb={"开始交易","保存记录","修改记录","停止交易"};
private String[] strl={"账号","商品","价格","现金支付"};
public home1()
{
JPanel arraybutton=new JPanel(new GridLayout(1,4));

for(int count=0;count<4;count++)
{buttons[count]=new JButton(strb[count]);
arraybutton.add(buttons[count]);
}
JPanel inputtable=new JPanel(new GridLayout(4,4));
for(int count=0;count<4;count++)
{ labels[count]=new JLabel(strl[count]);
inputtable.add(labels[count],count*4);
fields[count]=new JTextField();
inputtable.add(fields[count],count*4+1);
}
submit=new JButton("提交");
inputtable.add(submit,15);


Container container=getContentPane();
container.setLayout(new BorderLayout(5,5));
container.add(arraybutton,BorderLayout.NORTH);
container.add(inputtable,BorderLayout.CENTER);
setSize(500,500);
setVisible(true);

}
public static void main(String[] args)
{

home1 application=new home1();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}

}

麻烦高手指教
...全文
8622 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhiwenjing 2011-12-20
  • 打赏
  • 举报
回复
学习了!!我也有个问题 有人能帮我解决嘛?
这是我写的一个JFreechart做的图表例子,一直出现空指针异常错误,报错行线程启动行,求高手帮忙解决一下!
package com.mydesigner;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;
import org.jfree.util.UnitType;

import com.mydesigner.DynamicDataClass1.DemoPanel;

public class DynamicDataClass extends ApplicationFrame
{
public static Plot combineddomainxyplot;


static class DemoPanel extends JPanel implements ActionListener,Runnable
{
public static final int SUBPLOT_COUNT = 3;
private TimeSeriesCollection datasets[];
private double lastValue[];
static Thread thread1 ;
public void actionPerformed(ActionEvent actionevent)
{
for(int i = 0; i < 3; i++)
if(actionevent.getActionCommand().endsWith(String.valueOf(i)))
{
Millisecond millisecond1 = new Millisecond();
System.out.println("Now = " + millisecond1.toString());
lastValue[i] = lastValue[i] * (0.90000000000000002D + 0.20000000000000001D * Math.random());
datasets[i].getSeries(0).add(new Millisecond(), lastValue[i]);
}

if(actionevent.getActionCommand().equals("ADD_ALL"))
{
Millisecond millisecond = new Millisecond();
System.out.println("Now = " + millisecond.toString());
for(int j = 0; j < 3; j++)
{
lastValue[j] = lastValue[j] * (0.90000000000000002D + 0.20000000000000001D * Math.random());
datasets[j].getSeries(0).add(new Millisecond(), lastValue[j]);
}

}
}



public DemoPanel()
{
super(new BorderLayout());

// CombinedDomainXYPlot combineddomainxyplot = new CombinedDomainXYPlot(new DateAxis("Time"));
// datasets = new TimeSeriesCollection[3];
// for(int i = 0; i < 3; i++)
// {
// lastValue[i] = 100D;
// TimeSeries timeseries = new TimeSeries("Random " + i, DynamicDataClass1.class$org$jfree$data$time$Millisecond != null ? DynamicDataClass1.class$org$jfree$data$time$Millisecond : (DynamicDataClass1.class$org$jfree$data$time$Millisecond = DynamicDataClass1.getClass("org.jfree.data.time.Millisecond")));
// datasets[i] = new TimeSeriesCollection(timeseries);
// NumberAxis numberaxis = new NumberAxis("Y" + i);
// numberaxis.setAutoRangeIncludesZero(false);
// XYPlot xyplot = new XYPlot(datasets[i], null, numberaxis, new StandardXYItemRenderer());
// xyplot.setBackgroundPaint(Color.lightGray);
// xyplot.setDomainGridlinePaint(Color.white);
// xyplot.setRangeGridlinePaint(Color.white);
// combineddomainxyplot.add(xyplot);
// }

JFreeChart jfreechart = new JFreeChart("Dynamic Data Demo 3", combineddomainxyplot);
LegendTitle legendtitle = (LegendTitle)jfreechart.getSubtitle(0);
legendtitle.setPosition(RectangleEdge.RIGHT);
legendtitle.setMargin(new RectangleInsets(UnitType.ABSOLUTE, 0.0D, 4D, 0.0D, 4D));
jfreechart.setBorderPaint(Color.black);
jfreechart.setBorderVisible(true);
jfreechart.setBackgroundPaint(Color.white);
combineddomainxyplot.setBackgroundPaint(Color.lightGray);
((XYPlot) combineddomainxyplot).setDomainGridlinePaint(Color.white);
((XYPlot) combineddomainxyplot).setRangeGridlinePaint(Color.white);
//设置坐标轴到数据区的间距
((XYPlot) combineddomainxyplot).setAxisOffset(new RectangleInsets(4D, 4D, 4D, 4D));
//获得X轴数据
ValueAxis valueaxis = ((XYPlot) combineddomainxyplot).getDomainAxis();
//设置数据轴数据范围是否为自动
valueaxis.setAutoRange(true);
//设置数据轴固定数据范围
valueaxis.setFixedAutoRange(60000D);
ChartPanel chartpanel = new ChartPanel(jfreechart);
add(chartpanel);
JPanel jpanel = new JPanel(new FlowLayout());
for(int j = 0; j < 3; j++)
{
JButton jbutton1 = new JButton("Series " + j);
jbutton1.setActionCommand("ADD_DATA_" + j);
jbutton1.addActionListener(this);
jpanel.add(jbutton1);
}

JButton jbutton = new JButton("ALL");
jbutton.setActionCommand("ADD_ALL");
jbutton.addActionListener(this);
jpanel.add(jbutton);
add(jpanel, "South");
chartpanel.setPreferredSize(new Dimension(500, 470));
chartpanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
}

@Override
public void run() {
// TODO Auto-generated method stub

while(true){
try{
lastValue = new double[3];
CombinedDomainXYPlot combineddomainxyplot = new CombinedDomainXYPlot(new DateAxis("Time"));
datasets = new TimeSeriesCollection[3];
for(int i = 0; i < 3; i++)
{
lastValue[i] = 100D;
TimeSeries timeseries = new TimeSeries("Random " + i, DynamicDataClass1.class$org$jfree$data$time$Millisecond != null ? DynamicDataClass1.class$org$jfree$data$time$Millisecond : (DynamicDataClass1.class$org$jfree$data$time$Millisecond = DynamicDataClass1.getClass("org.jfree.data.time.Millisecond")));
datasets[i] = new TimeSeriesCollection(timeseries);
NumberAxis numberaxis = new NumberAxis("Y" + i);
numberaxis.setAutoRangeIncludesZero(false);
XYPlot xyplot = new XYPlot(datasets[i], null, numberaxis, new StandardXYItemRenderer());
xyplot.setBackgroundPaint(Color.lightGray);
xyplot.setDomainGridlinePaint(Color.white);
xyplot.setRangeGridlinePaint(Color.white);
combineddomainxyplot.add(xyplot);
Thread.sleep(300);
}
} catch(InterruptedException e) { }
}
}
}




@SuppressWarnings("null")
public static void startThread(){
Thread thread1 = null;

thread1.start();
System.out.print(thread1);
}
public DynamicDataClass(String s)
{
super(s);
setContentPane(new DemoPanel());
}

static Class getClass(String s) {
// TODO Auto-generated method stub
Class cls = null;
try{
cls = Class.forName(s);
}catch(ClassNotFoundException cnfe){
throw new NoClassDefFoundError(cnfe.getMessage());
}
return cls;
}

public static JPanel createDemoPanel()
{
return new DemoPanel();
}

public static void main(String args[])
{
DynamicDataClass1 dynamicdatademo3 = new DynamicDataClass1("Dynamic Data Demo 3");
dynamicdatademo3.pack();
RefineryUtilities.centerFrameOnScreen(dynamicdatademo3);
dynamicdatademo3.setVisible(true);
startThread();
}

// static Class _mthclass$(String s)
// {
// return Class.forName(s);
// ClassNotFoundException classnotfoundexception;
// classnotfoundexception;
// throw new NoClassDefFoundError(classnotfoundexception.getMessage());
}
报错信息:
Exception in thread "main" java.lang.NullPointerException
at com.mydesigner.DynamicDataClass.startThread(DynamicDataClass.java:165)
at com.mydesigner.DynamicDataClass.main(DynamicDataClass.java:196)
z2010490051 2011-10-12
  • 打赏
  • 举报
回复
import javax.swing.*;
import java.awt.*;
public class NullLayoutDemo extends JFrame{

private static final long serialVersionUID = -492496137935695467L;
private JLabel lbUser=new JLabel();
private JLabel lbPassword=new JLabel();
private JTextField tfUser=new JTextField();
private JTextField tfPassword=new JTextField();
private JButton btnLog=new JButton();
private Container container;

public NullLayoutDemo(){
super("空布局");
this.setSize(400,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String []args){
NullLayoutDemo frame=new NullLayoutDemo();
frame.setLayout();
frame.setVisible(true);
}
public void setLayout(){
container.setLayout(null);
lbUser.setText("用户名");
container.add(lbUser);
lbUser.setBounds(20,20,40,20);
container.add(tfUser);
tfUser.setBounds(70,20,250,20);
lbPassword.setText("密码");
container.add(lbPassword);
lbPassword.setBounds(20,20,40,20);
container.add(tfPassword);
tfPassword.setBounds(70,20,250,20);
btnLog.setText("登录");
container.add(btnLog);
btnLog.setBounds(140,20,20,20);

}

}
跟上面的错一样,怎么解决啊
愤怒的大闸蟹 2009-04-17
  • 打赏
  • 举报
回复
前面多少人都没仔细看程序,空指针是指你的JButton数组没有初始化!
buttons[count]=new JButton(strb[count])//这行在使用buttons的时候,buttons仅仅是声明了,但是多少个还没有初始化,在for前面加上一句
buttons = new JButton[4]就可以了。同样,你后面的labels数组和fields数据也是没有初始化,在for循环前面也加一个labels = new JLabel[4]
以及fields = new JTextField[4]就OK了。

另外,inputtable.add方法中的int是位置的索引,你现在的所以明显是不对的,肯定要出错的,要插入的容器位置超出了你的布局管理器声明的个数
耶律火柴 2009-04-17
  • 打赏
  • 举报
回复
成员对象会被初始化为null
网络咖啡 2009-04-17
  • 打赏
  • 举报
回复
对象数组也需要创建的啊
贝壳鱼 2009-04-17
  • 打赏
  • 举报
回复 3
数组作为一种内置对象, 默认的初始化也是NULL的, 所以你要对整个数组初始化后, 才能对里面的元素进行初始化。 里面的元素如果是基本数据类型, 那么当数组初始化后,里面的元素就自动赋初始化值了。 但是如果里面存的是对象的引用, 那么所有引用当数组初始化后, 还是为NULL, 要分别对里面的对象进行初始化。
ailcxf 2009-04-16
  • 打赏
  • 举报
回复
j2me没有句柄类型,是类就得初始哈
hcz883867 2009-04-16
  • 打赏
  • 举报
回复
嗯嗯。数据都要初始化一下。
JarodYv 2009-04-16
  • 打赏
  • 举报
回复
你的buttons,labels,fields这些数组都没有初始化,当然会报空指针。

13,100

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧