Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6866755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:13:29+00:00 2026-05-27T03:13:29+00:00

NetBeans. UI created using IDE. My implementation In order of appearance: 1 global form

  • 0

NetBeans. UI created using IDE.
My implementation In order of appearance:

1 global form variable:

private DefaultListModel model;

2 constructor:

ArrayList<String> cameras = repository.getCameraNames();

model= new DefaultListModel();
for (int i = 0; i < cameras.size(); i++) {
    model.addElement(cameras.get(i));
}
thelist.setModel(model);

3 remove button:

private void btnRemoverActionPerformed(java.awt.event.ActionEvent evt) {                                           
    int index = thelist.getSelectedIndex();

    model.removeElementAt(index);
}

On the removeElementAt line, I get NullPointerException. If i change the index for a explicit number, it works but doesn’t with the index variable. But the selected index, doesn’t work!

Anyone can help?

Oh, and here is some stackttrace:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at FrmPrincipal.thelistValueChanged(FrmPrincipal.java:217)
at FrmPrincipal.access$000(FrmPrincipal.java:22)
at FrmPrincipal$1.valueChanged(FrmPrincipal.java:77)
at javax.swing.JList.fireSelectionValueChanged(JList.java:1798)
at javax.swing.JList$ListSelectionHandler.valueChanged(JList.java:1812)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:184)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:164)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:211)
at javax.swing.DefaultListSelectionModel.removeIndexInterval(DefaultListSelectionModel.java:677)
at javax.swing.plaf.basic.BasicListUI$Handler.intervalRemoved(BasicListUI.java:2601)
at javax.swing.AbstractListModel.fireIntervalRemoved(AbstractListModel.java:179)
at javax.swing.DefaultListModel.removeElementAt(DefaultListModel.java:332)

UPDATE

I fixed by changing the following method:

private void thelistValueChanged(javax.swing.event.ListSelectionEvent evt) {
    txtCameraName.setText(thelist.getSelectedValue().toString());
}

To

private void thelistValueChanged(javax.swing.event.ListSelectionEvent evt) {
    txtCameraName.setText((String)thelist.getSelectedValue());
}

But I don’t know why!
Can someone explain why it didn’t accept the toString() and accepted the casting?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:13:30+00:00Added an answer on May 27, 2026 at 3:13 am

    Your change “fixed” your problem because getSelectedValue().toString() will throw a NPE if there is no selection, whereas (String)getSelectedValue() will evaluate to null if there is no selection. But, if you ever put things in your list that aren’t Strings, then you’ll get ClassCastExceptions when you try to cast the selected value to a String. So, this may have solved your current problem, but it’s not a solution really.

    I would just do this:

    private void theListValueChanged(ListSelectionEvent e) {
        final Object selectedValue = theList.getSelectedValue();
        if ( selectedValue != null ) {
            txtCameraName.setText( selectedValue.toString() );
        } else {
            // Clear the text since there's no selection
            txtCameraName.setText( null );
        }   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using NetBeans IDE and have created a new project. The new project
I'm using NetBeans IDE to create a system. I created an instance of a
I created a JApplet using Netbeans 6.5.1 that accesses a database. When the program
I have created a webapp using maven2 archetype in netbeans 6.7. when I do
I'm using Netbeans 6.7.1 on Windows. I created a Java application using Swing. When
I am using Netbeans 6.8 and Glassfish v3.0. I created an ejb module and
I am creating a simple application to learn Hibernate. I am using NetBeans IDE
I have created a GUI in Java using swings with the help of Netbeans
I am using Netbeans 7.0 IDE to create Entity Classes from Database. When I
I'm using NetBeans IDE to develop a project for my senior year. The project

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.