Hot questions for Using JTextField in nullpointerexception

Top Java Programmings / JTextField / nullpointerexception

Question:


Answer:

The problem is that your class member JTextField textField is never initialized, yet you are trying to use it within your actionPerformed

Inside your go() method you create a new JTextField textField that has nothing do to with the original textField.

In fact inside go() you basically create new objects

JFrame frame = new JFrame();
JTextField textField = new JTextField("Type here);
JButton button = new JButton("Send");

Instead of initializing those already defined for your class.

Question:

I use a document listener and it brings me a null pointer exception . How could I stop this exception . I use this for search some content of a table. With using this I could search relevant content . Also this brings me a null pointer exception .

These are the steps,

In the beginning,

Vector originalTableModel;
DocumentListener documentListener;

In the counstructor,

originalTableModel = (Vector) ((DefaultTableModel) list_table.getModel()).getDataVector().clone();
//add document listener to jtextfield to search contents as soon as something typed on it
addDocumentListener();

My documentlistener method,

private void addDocumentListener(){
   documentListener = new DocumentListener(){
      public void changedUpdate(DocumentEvent documentEvent){
           search();
      }

      public void insertUpdate(DocumentEvent documentEvent){
           search();
      }

      public void removeUpdate(DocumentEvent documentEvent){
           search();
      }

      private void search(){
           searchTableContents(search_field.getText());
      }

   };
}

My searching Method,

public void searchTableContents(String searchString)
{
    DefaultTableModel currtableModel = (DefaultTableModel) list_table.getModel();
    //To empty the table before search
    currtableModel.setRowCount(0);
    //To search for contents from original table content
    for (Object rows : originalTableModel)
    {
        Vector rowVector = (Vector) rows;
        for (Object column : rowVector)
        {

                if **(column.toString().toLowerCase().contains(searchString.toLowerCase())**)
                {

                    //content found so adding to table
                    currtableModel.addRow(rowVector);
                    break;
                }


        }
    }
}

This is the place where I called this method,

private void search_fieldKeyReleased(java.awt.event.KeyEvent evt)                                                      
{                                                          
    // TODO add your handling code here:
    searchTableContents(search_field.getText());
}

I bold the point where I got this null pointer exception.

Have any ideas ?


Answer:

Most likely your column variable is null. This will be the case if at least one of the cells in the table is empty. Furthermore, since originalTableModel is taken as a clone of the table model in the constructor, any changes since the clone was taken till the time searchTableContents is run will not be reflected in the originalTableModel.

To fix it, you should change the if to if (column != null && column.toString().toLowerCase().contains(searchString.toLowerCase())).

Question:


Answer:

Seems like you try to parse an empty String because tPages = "0"; is never called.

Use tPages.isEmpty() in your if statement instead of tPages == ""

Question:

I want to add many text fields for my project and decided to just use arrays because all those 22 text fields have the same attributes.

I had tried JTextField txtFld[] = new JTextField[5];. it has no parameter errors but when I try to run my program, this shows up in the console

Exception in thread "main" java.lang.NullPointerException

I don't want to use

JTextField txtFld[] = new JTextField[]{
    new JTextField(10),
    ...
    ...
    ... // repeat the same code about 20 times
};

Because it takes up so many lines and the text fields are of the same type anyway. All I want to do is an array of text fields with the same length and just use mainPanel(txtFld[0]); ... mainPanel(txtFld[21]);

Is it possible to create a text field array in just a single line of code?


Answer:

As I'm new to SO, I could just answer the question but it's a repeated question. I'm writing the answer:

It's unclear is FIELDS is your JTextField aray or a constant. If it is the component array itself, consider using the .length array field when iterating. This reduces code maintenance:

Container c = getContentPane();
JTextField[] fields = new JTextField[22];
for (int i = 0; i < 22; i++) {
   fields[i] = new JTextField("Your Value");
   //you can add any listener for JTextField here
   c.add(fields[i]);
}

Note uppercase variables are used for constants under Java naming conventions.

The answer's been copied from here. I couldn't mark this question as duplicate.