Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. JList is returning null value

JList is returning null value

Scheduled Pinned Locked Moved Java
questiondatabasehelpdiscussion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sachin k Rajput
    wrote on last edited by
    #1

    I'm showing or selecting images from a list. My code for listselectionlistener is:

    listener = new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
    
          index = imageList.getSelectedIndex();
            imageList.setSelectedIndex(index);
                  o = imageList.getSelectedValue();
                if (o instanceof BufferedImage) {
                    imageView.setIcon(new ImageIcon((BufferedImage)o));
                   
            }
    
            }
    
        };
    
       imageList.addListSelectionListener(listener);
    

    to get next item in JList (imageList) I'm coding like this:

    public void nextimage()
    {
    index ++;

    imageList.addListSelectionListener(listener);

       imageList.setSelectionMode(ListSelectionModel.SINGLE\_SELECTION); 
       imageList.setSelectedIndex(index); -----here is error: Null Pointer Exception
    

    }

    I initialize index as int index=0; Program in starting returning right value but after 4 or 5 images it's not working. How can I change the Item in this list? Please suggest your opinions.

    T 1 Reply Last reply
    0
    • S Sachin k Rajput

      I'm showing or selecting images from a list. My code for listselectionlistener is:

      listener = new ListSelectionListener() {

              @Override
              public void valueChanged(ListSelectionEvent e) {
      
            index = imageList.getSelectedIndex();
              imageList.setSelectedIndex(index);
                    o = imageList.getSelectedValue();
                  if (o instanceof BufferedImage) {
                      imageView.setIcon(new ImageIcon((BufferedImage)o));
                     
              }
      
              }
      
          };
      
         imageList.addListSelectionListener(listener);
      

      to get next item in JList (imageList) I'm coding like this:

      public void nextimage()
      {
      index ++;

      imageList.addListSelectionListener(listener);

         imageList.setSelectionMode(ListSelectionModel.SINGLE\_SELECTION); 
         imageList.setSelectedIndex(index); -----here is error: Null Pointer Exception
      

      }

      I initialize index as int index=0; Program in starting returning right value but after 4 or 5 images it's not working. How can I change the Item in this list? Please suggest your opinions.

      T Offline
      T Offline
      TorstenH
      wrote on last edited by
      #2

      You need to check if the JList does contain so much values. You're not coding safe. You need to take more care. You can never expect a value to be there as you need it - always check if the value is valid before you use it!

      regards Torsten When I'm not working

      S 1 Reply Last reply
      0
      • T TorstenH

        You need to check if the JList does contain so much values. You're not coding safe. You need to take more care. You can never expect a value to be there as you need it - always check if the value is valid before you use it!

        regards Torsten When I'm not working

        S Offline
        S Offline
        Sachin k Rajput
        wrote on last edited by
        #3

        Thanks for your quick reply. Will you be more specific on checking the value? As I'm selecting the value from Jlist that is showing thumbnails. I'm not getting the exact way for selecting next item.:confused:

        S 1 Reply Last reply
        0
        • S Sachin k Rajput

          Thanks for your quick reply. Will you be more specific on checking the value? As I'm selecting the value from Jlist that is showing thumbnails. I'm not getting the exact way for selecting next item.:confused:

          S Offline
          S Offline
          Shubhashish_Mandal
          wrote on last edited by
          #4

          "TorstenH" want to say you that you have to check the "index" value before using it to set the selected item. You should check "index" value whether it is in the range of your image list size.

          Regards Shubhashish

          S 1 Reply Last reply
          0
          • S Shubhashish_Mandal

            "TorstenH" want to say you that you have to check the "index" value before using it to set the selected item. You should check "index" value whether it is in the range of your image list size.

            Regards Shubhashish

            S Offline
            S Offline
            Sachin k Rajput
            wrote on last edited by
            #5

            Well I did this. The only problem was SwingUtlities.InvokeLater().

            T 1 Reply Last reply
            0
            • S Sachin k Rajput

              Well I did this. The only problem was SwingUtlities.InvokeLater().

              T Offline
              T Offline
              TorstenH
              wrote on last edited by
              #6

              That's a function, not a problem. The problem would be that you tried to update the GUI while you where using it. SwingUtilities.InvokeLater()[^] would cause the Thread to wait and update the GUI when possible.

              regards Torsten When I'm not working

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups