Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java如何在另一个方法的方法中使用变量_Java_Methods - Fatal编程技术网

java如何在另一个方法的方法中使用变量

java如何在另一个方法的方法中使用变量,java,methods,Java,Methods,我尝试过类似的方法:但出现错误: 严重:空 无效的 com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:列“MYUSER”不能为null 而它不应该?另外,我想问一下,在我的程序中是否有一些不好的做法。 有什么建议吗 /* * To change this license header, choose License Headers in Project Properties. * To cha

我尝试过类似的方法:但出现错误:

严重:空 无效的 com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:列“MYUSER”不能为null

而它不应该?另外,我想问一下,在我的程序中是否有一些不好的做法。 有什么建议吗

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */



public class Main extends JFrame  {


   String text;

  public Main() throws HeadlessException {

    //opis
    JLabel opis = new JLabel("Opis: ");
    JTextField opisTextField = new JTextField();
    opisTextField.setPreferredSize(new Dimension(100, 20));
    add(opis);
    add(opisTextField);



//jak klikniemy w przycisk program doda wartosci wpisane do bazy danych.
    Button z = new Button("Dodaj do bazy danych"); 

     add(z);
    z.addMouseListener(new MouseAdapter() {

      @Override
      public void mouseClicked(MouseEvent e){}
      @Override
            public void mouseEntered(MouseEvent e){}
      @Override
            public void mouseExited(MouseEvent e){}
      @Override
            public void mouseReleased(MouseEvent e){}
      @Override
     public void mousePressed(MouseEvent e) {


        Main m = new Main();
        m.text = opisTextField.getText();






    });

  }

public void  readDataBase()  throws Exception {
    try {
      // this will load the MySQL driver, each DB has its own driver
      Class.forName("com.mysql.jdbc.Driver");
      // setup the connection with the DB.
      connect = DriverManager
          .getConnection("jdbc:mysql://localhost/feedback?"
              + "user=sqluser&password=sqluserpw");

      // statements allow to issue SQL queries to the database
      statement = connect.createStatement();
      // resultSet gets the result of the SQL query


      // preparedStatements can use variables and are more efficient
      preparedStatement = connect
          .prepareStatement("insert into  FEEDBACK.COMMENTS values (default, ?, ?, ?, ? , ?, ?)");
        // "myuser, webpage, datum, summary, COMMENTS from FEEDBACK.COMMENTS");
        // parameters start with 1

     preparedStatement.setString(1, text);

      preparedStatement.executeUpdate();



      // remove again the insert comment


    } catch (ClassNotFoundException | SQLException e) {
      throw e;
    } finally {
     // close();
    }

  }

  public static void main(String[] args) throws Exception {

    new Main().setVisible(true);

  }

}

抱歉,无法浏览所有代码,但看起来您声明了
字符串文本preparedStatement.setString(1,text)根据错误不能为空。

请不要粘贴100行代码。相反,构建一个小示例来演示这个问题。我想有人正在做作业:坏习惯的一个方面是代码没有正确缩进。因为你不能这样做。你的标题与异常完全无关。谢谢,我通过替换Main m=new Main()修复了它;m、 text=opisTextField.getText();使用text=opisTextField.getText();很奇怪,因为它以前没有像那样工作过。