qq_33331562 2016-08-08 00:53 采纳率: 0%
浏览 800

请问我的代码哪里有空指针异常?

package com.lwy;

import java.awt.*;
import javax.swing.*;

public class Window extends JFrame {
public static void main(String[] args) {
Window w = new Window();
}

// 文本框
private JTextField t_size;
private JTextArea t_ratio;
private JTextArea t_value;
private JTextArea t_initVector;
private JTextField t_error;
private JTextArea t_answer;

// 按钮
private JButton bt_caculate;
private JButton bt_reset;

// 小容器
private JLabel jl1;
private JLabel jl2;
private JLabel jl3;
private JLabel jl4;
private JLabel jl5;
private JLabel jl6;
private JLabel jl7;
private JLabel jl8;

// 构造方法
public Window() {
    super();
    this.setTitle("高斯赛德尔迭代法求解线性方程组");
    Init();
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // 设置布局方式为绝对定位
    this.setLayout(null);
    this.setBounds(0, 0, 600, 500);
    // 设置窗体的标题图标
    Image img = new ImageIcon("E:/set.png").getImage();
    this.setIconImage(img);
    // 设置窗体的大小可以改变
    this.setResizable(true);
    // 居中显示
    this.setLocationRelativeTo(null);

    this.setVisible(true);
}

public void Init() {
    Container con = this.getContentPane();
    // 设置背景图片
    jl1 = new JLabel();
    Image img1 = new ImageIcon("E:/backgroundImg.jpg").getImage();
    jl1.setIcon(new ImageIcon(img1));
    jl1.setBounds(0, 0, 600, 500);
    // 欢迎标题
    jl2 = new JLabel("欢迎使用高斯赛德尔迭代法求解线性方程组");
    jl2.setBounds(130, 10, 350, 30);
    jl2.setFont(new Font("宋体", Font.BOLD, 16));
    // 维度
    jl3 = new JLabel("请输入方程组的维度:");
    jl3.setBounds(20, 40, 150, 20);
    jl3.setFont(new Font("宋体", Font.BOLD, 14));
    t_size = new JTextField();
    t_size.setBounds(170, 40, 250, 25);
    // 系数矩阵
    jl4 = new JLabel("请输入系数矩阵:");
    jl4.setBounds(20, 70, 150, 20);
    jl4.setFont(new Font("宋体", Font.BOLD, 14));
    t_ratio = new JTextArea();
    t_ratio.setBounds(170, 70, 250, 100);
    // 值向量
    jl5 = new JLabel("请输入方程的值向量:");
    jl5.setBounds(20, 250, 150, 20);
    jl5.setFont(new Font("宋体", Font.BOLD, 14));
    t_ratio = new JTextArea();
    t_ratio.setBounds(170, 250, 250, 25);
    // 迭代初始向量
    jl6 = new JLabel("请输入迭代初始向量:");
    jl6.setBounds(20, 280, 150, 20);
    jl6.setFont(new Font("宋体", Font.BOLD, 14));
    t_initVector = new JTextArea();
    t_initVector.setBounds(170, 280, 250, 25);
    // 误差
    jl7 = new JLabel("请输入误差范围:");
    jl7.setBounds(20, 310, 150, 20);
    jl7.setFont(new Font("宋体", Font.BOLD, 14));
    t_error = new JTextField();
    t_error.setBounds(170, 310, 250, 25);
    // 解向量
    jl8 = new JLabel("运算结果为:");
    jl8.setBounds(20, 340, 150, 20);
    jl8.setFont(new Font("宋体", Font.BOLD, 14));
    t_answer = new JTextArea();
    t_answer.setBounds(170, 340, 250, 25);

    // 计算按钮
    bt_caculate = new JButton("计算");
    bt_caculate.setBounds(170, 400, 70, 30);
    // 重置按钮
    bt_reset = new JButton("重置");
    bt_reset.setBounds(350, 400, 70, 30);

    jl1.add(jl2);
    jl1.add(jl3);
    jl1.add(jl4);
    jl1.add(jl5);
    jl1.add(jl6);
    jl1.add(jl7);
    jl1.add(jl8);
    jl1.add(bt_caculate);
    jl1.add(bt_reset);
    con.add(jl1);
    con.add(t_size);
    con.add(t_ratio);
    con.add(t_value);
    con.add(t_initVector);
    con.add(t_error);
    con.add(t_answer);
}

}
错误:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1093)
at java.awt.Container.add(Container.java:417)
at com.lwy.Window.Init(Window.java:120)
at com.lwy.Window.(Window.java:37)
at com.lwy.Window.main(Window.java:8)

  • 写回答

5条回答 默认 最新

  • kun_hello 2016-08-08 01:23
    关注

    Init方法 120行 是哪一行? 在120行有空值

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?