空指针异常求解决
package com.newer.TextSwing;import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.tree.DefaultMutableTreeNode;
public class TextSwing_015 {
public static void main(String []args){
new TextTree();
}
}
class TextTree extends JFrame{
JTree tree;
String message;
DefaultMutableTreeNode company,boss,sunzong,manager,xiaowu,
laoer,imployer,pangzi,shouzi,gaozi;
public TextTree(){
boss=new DefaultMutableTreeNode("老板");
sunzong=new DefaultMutableTreeNode(new Emp("孙总",38,"女",50000));
manager=new DefaultMutableTreeNode("经理");
xiaowu=new DefaultMutableTreeNode(new Emp("小武",28,"男",20000));
laoer=new DefaultMutableTreeNode(new Emp("老二",30,"男",22000));
imployer=new DefaultMutableTreeNode("员工");
pangzi=new DefaultMutableTreeNode(new Emp("胖子",22,"男",6000));
shouzi=new DefaultMutableTreeNode(new Emp("瘦子",24,"男",6800));
gaozi=new DefaultMutableTreeNode(new Emp("羔子",18,"男",4500));
tree=new JTree(company);
company.add(boss);
company.add(manager);
company.add(imployer);
boss.add(sunzong);
manager.add(xiaowu);
manager.add(laoer);
imployer.add(pangzi);
imployer.add(shouzi);
imployer.add(gaozi);
this.setTitle("公司员工表");
this.add(tree);
this.setBounds(100, 100, 400, 300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class Emp{
String name;
int age;
String 易做图;
int pay;
public Emp(String name,int age,String 易做图,int pay){
this.name=name;
this.age=age;
this.易做图=易做图;
this.pay=pay;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return 易做图;
}
public void setSex(String 易做图) {
this.易做图 = 易做图;
}
public int getPay() {
return pay;
}
public void setPay(int pay) {
this.pay = pay;
}
public String toString() {
return "name=" + name + ", age=" + age + ", pay=" + pay + ", 易做图="
+ 易做图 ;
}
} --------------------编程问答-------------------- 错误信息应该有第几行报的空指针啊,直接去找就行了
不过,楼主这结帖率有点...
祝你好运 --------------------编程问答-------------------- company.add(boss);和那个new TextTree();这个两行报错,我怎么看都没错误呢
--------------------编程问答-------------------- 你在出错的地方的前面 添加
company = new DefaultMutableTreeNode("初始化company");
注意结贴呀。楼主,楼上都在抱怨你了 --------------------编程问答--------------------
可以理解,你要看他发了几个贴。。 --------------------编程问答--------------------
可以理解,你要看他发了几个贴。。(靠,刚才看错地方了) --------------------编程问答-------------------- 2L已经说出错误的地方了,company没有初始化,所以
company.add(boss); 调用company对象的方法的时候出空指针异常了。
--------------------编程问答-------------------- 拿分走人 --------------------编程问答-------------------- DefaultMutableTreeNode company,boss,sunzong,manager,xiaowu,
laoer,imployer,pangzi,shouzi,gaozi;
既然相同的定义,boss都初始化了,你的company没有,一直是NULL,你怎么插值呢? --------------------编程问答-------------------- 空指针其实很简单,非常简单,你就看看出现空指针的地方,那个“.”之前的变量铁定是未经过初始化的,看看它是怎么来的,为什么没有初始化。实在找不到就用eclipse的debug找一找。 --------------------编程问答-------------------- 就是这里,company没有初始化。给他new一个 --------------------编程问答-------------------- --------------------编程问答-------------------- 顶一下就可以拿分?
补充:Java , Java SE