首页 \ 问答 \ Java异常nullpointerexception错误(Java Exception nullpointerexception error)

Java异常nullpointerexception错误(Java Exception nullpointerexception error)

什么导致null异常? 我根本无法看到它..请第二组眼睛将帮助这个初学者java学生..这个应用程序基本上捕获有关游戏团队的5条信息并插入到mySQL数据库中。

错误如下所示:InsertTeamInfo.access $ 100中InsertTeamInfo.insertRecord(InsertTeamInfo.java:130)中的线程“AWT-EventQueue-0”java.lang.NullPointerException异常(InsertTeamInfo.java:11)

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

public class InsertTeamInfo extends JFrame{
  private String sql;
  private Connection con;
  private Statement stat;
  private JButton btnSave;

  private JTextField txtTeam;  
  private JTextField txtCity;
  private JTextField txtYear;
  private JTextField txtLoserTeam;
  private JTextField txtLoserCity;

  private JLabel lblTeam;
  private JLabel lblCity;
  private JLabel lblYear;
  private JLabel lblLoserTeam;
  private JLabel lblLoserCity;    

  public InsertTeamInfo(){
    super("Favorite Team");

    btnSave = new JButton("Save");

    txtTeam = new JTextField("");
    txtCity = new JTextField("");
    txtYear = new JTextField("");
    txtLoserTeam = new JTextField("");
    txtLoserCity = new JTextField("");

    lblTeam = new JLabel("Team");
    lblCity = new JLabel("City");
    lblYear = new JLabel("Year Played");
    lblLoserTeam = new JLabel("Loser Team");
    lblLoserCity = new JLabel("Loser City");

    txtYear.setEditable(true);
    txtLoserTeam.setEditable(true);
    txtLoserCity.setEditable(true);
    txtTeam.setEditable(true);
    txtCity.setEditable(true);  

  }

  public void launchJFrame(){
    //width - height
    setSize(500, 300);
    getContentPane().setLayout(null);
    getContentPane().setBackground(Color.white);

    setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    getContentPane().add(btnSave);

    getContentPane().add(txtTeam);
    getContentPane().add(txtCity);
    getContentPane().add(txtYear);
    getContentPane().add(txtLoserTeam);
    getContentPane().add(txtLoserCity);

    getContentPane().add(lblTeam);
    getContentPane().add(lblCity);    
    getContentPane().add(lblYear);
    getContentPane().add(lblLoserTeam);
    getContentPane().add(lblLoserCity);

    lblTeam.setBounds(new Rectangle(65,20,100,30));
    lblCity.setBounds(new Rectangle(65,55,100,30));
    lblYear.setBounds(new Rectangle(65, 85, 100, 30));
    lblLoserTeam.setBounds(new Rectangle(65, 115, 100, 30));
    lblLoserCity.setBounds(new Rectangle(65, 145, 100, 30));

    txtTeam.setBounds(new Rectangle(210, 20, 150, 30));
    txtCity.setBounds(new Rectangle(210, 54, 150, 30));
    txtYear.setBounds(new Rectangle(210, 86, 150, 30));
    txtLoserTeam.setBounds(new Rectangle(210, 119, 150, 30));
    txtLoserCity.setBounds(new Rectangle(210, 150, 150, 30));

    btnSave.setBounds(new Rectangle(65, 190, 90, 30));
    setVisible(true);

    addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(java.awt.event.WindowEvent evt) {
        shutDown();
      }
    });

    btnSave.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){ //Handle Save button click event
            insertRecord();
          }
     });
 }

 private void insertRecord(){
    try {
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/week5", "root","Saras231");
    }
    catch(Exception ex){
      JOptionPane.showMessageDialog( null, "Error connection to database.");         
      System.exit(0);
    }

    try {
       sql = "INSERT INTO Wins_S001 (Team, City, Year_T, LoserTeam, LoserCity) VALUES (?, ?, ?, ?, ?)";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setString(1, txtTeam.getText());
        preparedStatement.setString(2, txtCity.getText());
        preparedStatement.setString(3, txtYear.getText());
        preparedStatement.setString(4, txtLoserTeam.getText());
        preparedStatement.setString(5, txtLoserCity.getText());
        preparedStatement.executeUpdate(); 
       JOptionPane.showMessageDialog( null, "Data Inserted Successfully");          
    }
    catch(SQLException ex){
      ex.printStackTrace();
      JOptionPane.showMessageDialog( null, "Data Insert failed.");          
    }

    try{
      stat.close();
      con.close();
    }
    catch(SQLException ex){
      JOptionPane.showMessageDialog( null, "All active connection closed to the database.");          
    }

 }

  private void shutDown(){
    int returnVal = JOptionPane.showConfirmDialog(this, "Are you sure you want to quit?");

    if(returnVal == JOptionPane.YES_OPTION){

      System.exit(0);
    }
  }

  public static void main(String[] args){
    InsertTeamInfo gui = new InsertTeamInfo();
    gui.launchJFrame();
  }
}

What is causing the null exception here? I am simply unable to see it.. please second set of eyes would help this beginner java student..This application basically captures 5 pieces of info about a game team and insert into mySQL database.

The error is as below: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at InsertTeamInfo.insertRecord(InsertTeamInfo.java:130) at InsertTeamInfo.access$100(InsertTeamInfo.java:11)

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

public class InsertTeamInfo extends JFrame{
  private String sql;
  private Connection con;
  private Statement stat;
  private JButton btnSave;

  private JTextField txtTeam;  
  private JTextField txtCity;
  private JTextField txtYear;
  private JTextField txtLoserTeam;
  private JTextField txtLoserCity;

  private JLabel lblTeam;
  private JLabel lblCity;
  private JLabel lblYear;
  private JLabel lblLoserTeam;
  private JLabel lblLoserCity;    

  public InsertTeamInfo(){
    super("Favorite Team");

    btnSave = new JButton("Save");

    txtTeam = new JTextField("");
    txtCity = new JTextField("");
    txtYear = new JTextField("");
    txtLoserTeam = new JTextField("");
    txtLoserCity = new JTextField("");

    lblTeam = new JLabel("Team");
    lblCity = new JLabel("City");
    lblYear = new JLabel("Year Played");
    lblLoserTeam = new JLabel("Loser Team");
    lblLoserCity = new JLabel("Loser City");

    txtYear.setEditable(true);
    txtLoserTeam.setEditable(true);
    txtLoserCity.setEditable(true);
    txtTeam.setEditable(true);
    txtCity.setEditable(true);  

  }

  public void launchJFrame(){
    //width - height
    setSize(500, 300);
    getContentPane().setLayout(null);
    getContentPane().setBackground(Color.white);

    setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    getContentPane().add(btnSave);

    getContentPane().add(txtTeam);
    getContentPane().add(txtCity);
    getContentPane().add(txtYear);
    getContentPane().add(txtLoserTeam);
    getContentPane().add(txtLoserCity);

    getContentPane().add(lblTeam);
    getContentPane().add(lblCity);    
    getContentPane().add(lblYear);
    getContentPane().add(lblLoserTeam);
    getContentPane().add(lblLoserCity);

    lblTeam.setBounds(new Rectangle(65,20,100,30));
    lblCity.setBounds(new Rectangle(65,55,100,30));
    lblYear.setBounds(new Rectangle(65, 85, 100, 30));
    lblLoserTeam.setBounds(new Rectangle(65, 115, 100, 30));
    lblLoserCity.setBounds(new Rectangle(65, 145, 100, 30));

    txtTeam.setBounds(new Rectangle(210, 20, 150, 30));
    txtCity.setBounds(new Rectangle(210, 54, 150, 30));
    txtYear.setBounds(new Rectangle(210, 86, 150, 30));
    txtLoserTeam.setBounds(new Rectangle(210, 119, 150, 30));
    txtLoserCity.setBounds(new Rectangle(210, 150, 150, 30));

    btnSave.setBounds(new Rectangle(65, 190, 90, 30));
    setVisible(true);

    addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(java.awt.event.WindowEvent evt) {
        shutDown();
      }
    });

    btnSave.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){ //Handle Save button click event
            insertRecord();
          }
     });
 }

 private void insertRecord(){
    try {
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/week5", "root","Saras231");
    }
    catch(Exception ex){
      JOptionPane.showMessageDialog( null, "Error connection to database.");         
      System.exit(0);
    }

    try {
       sql = "INSERT INTO Wins_S001 (Team, City, Year_T, LoserTeam, LoserCity) VALUES (?, ?, ?, ?, ?)";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setString(1, txtTeam.getText());
        preparedStatement.setString(2, txtCity.getText());
        preparedStatement.setString(3, txtYear.getText());
        preparedStatement.setString(4, txtLoserTeam.getText());
        preparedStatement.setString(5, txtLoserCity.getText());
        preparedStatement.executeUpdate(); 
       JOptionPane.showMessageDialog( null, "Data Inserted Successfully");          
    }
    catch(SQLException ex){
      ex.printStackTrace();
      JOptionPane.showMessageDialog( null, "Data Insert failed.");          
    }

    try{
      stat.close();
      con.close();
    }
    catch(SQLException ex){
      JOptionPane.showMessageDialog( null, "All active connection closed to the database.");          
    }

 }

  private void shutDown(){
    int returnVal = JOptionPane.showConfirmDialog(this, "Are you sure you want to quit?");

    if(returnVal == JOptionPane.YES_OPTION){

      System.exit(0);
    }
  }

  public static void main(String[] args){
    InsertTeamInfo gui = new InsertTeamInfo();
    gui.launchJFrame();
  }
}

原文:
更新时间:2022-05-17 21:05

最满意答案

使用

#links a {clear:left;float:left}

float将允许链接的大小, clear将阻止链接在同一行上。

您可能需要在#links容器中添加一个clear:left ,具体取决于您的设计。

编辑

你提问的一个小教程:

有两种类型的元素,内联和块。 内联线显示没有中断。 块元素占据了整条线并转移到下一个元素。

内联元素不能具有其宽度或高度样式。 块可以。

<a>是内联元素。 通过将其显示设置为阻止,您可以告诉它每次都创建一个新行。

float给出了元素内联行为,因此它们会在彼此旁边碰撞并流到下一行。 float还允许您设置元素的宽度/高度。 这是两者之间的混合。

clear属性停止内联浮动并返回到正常的块行为(每次都有新行)。

您不需要display:blockfloat:在同一时间。

另一种解决方案将涉及display:inline-block ,但这在几个浏览器中不受支持,因此不受鼓励(尽管我觉得它非常方便)。


Use

#links a {clear:left;float:left}

The float will allow the link to be sized, and the clear will prevent the links from being on the same line.

You may need to add a clear:left to the #links container depending on your design.

EDIT

A little tutorial since you asked:

There are two types of elements, inline and block. Inline ones show in a line with no breaks. Block elements take up the whole line and move to the next one.

Inline elements can't have their width or height styled. Blocks can.

<a> is an inline element. By setting its display to block, you tell it to make a new line every time.

float gives elements inline behavior so they bump up next to eachother and flow over onto the next line. float also allows you to style the width/height of the element. It's sort of a mix between the two.

The clear attribute stops the inline floating and goes back to normal block behavior (new lines every time).

You won't need display:block and float: at the same time.

Another solution would involve display:inline-block, but this is not supported in several browsers so isn't encouraged (although I find it pretty handy).

相关问答

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)