0

I edited the whole class.I try to convert an ItemID which is displayed in displayArea JTextArea(is redirected here from system.out) to an ItemName which is displayed in a messagedialog box after I press button "Get".I've got a Hashtable table to do the conversion.Here is part of my code:

public class HashtableTest2 extends JFrame {
   private JLabel statusLabel;
   private Hashtable<String,String> table;
   private JTextArea displayArea;
   private JTextField  movieNameField;
   private JTextField firstNameField;

   @SuppressWarnings("unused")
private PrintStream standardOut;
   private JTextArea textField;
   private JTextField textField_1;

   // set up GUI to demonstrate Hashtable features
   public HashtableTest2()
   {


      super( "Movie Recommender" );

      statusLabel = new JLabel();
      table = new Hashtable<String,String>();
      displayArea = new JTextArea( 4, 20 );
      displayArea.setTabSize(8);
      displayArea.setEditable( false );
     //Redirect console to displayArea
      PrintStream printStream = new PrintStream(new CustomOutputStream(displayArea));
      standardOut = System.out;
      System.setOut(printStream);
      System.setErr(printStream);
      JPanel northSubPanel = new JPanel();

      northSubPanel.add( new JLabel( "ID" ) );
      firstNameField = new JTextField( 8 );
      northSubPanel.add( firstNameField );

      northSubPanel.add( new JLabel( " movie name (key)" ) );
      movieNameField = new JTextField( 8 );
      northSubPanel.add(  movieNameField );

      JPanel northPanel = new JPanel();
      northPanel.setLayout( new BorderLayout() );
      northPanel.add( northSubPanel, BorderLayout.NORTH );
      northPanel.add( statusLabel, BorderLayout.SOUTH );

      JPanel southPanel = new JPanel();
      southPanel.setLayout( new GridLayout( 2, 5 ) );

      // adds event handler for ID textArea displayed in MovieName textArea
      movieNameField.addKeyListener(new KeyListener() {

        @Override
        public void keyPressed(KeyEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyReleased(KeyEvent arg0) {


                   table.put("GoldenEye","2");
                   table.put("Four Rooms",  "3");
                   table.put("Get Shorty","4");
                   table.put("Copycat","5");
                   table.put("Shanghai Triad","6");
                   table.put("Twelve Monkeys","7");
                   table.put("Babe","8");
                   table.put("Dead Man Walking","9");
                   table.put("Richard III","10");
                   table.put("204", "Back");
                   table.put("5", "Copycat");
                   table.put("7", "Twelve Monkeys");
        }

        @Override
        public void keyTyped(KeyEvent e) {
            // TODO Auto-generated method stub

        }

      });

      // button to get value for specific key
      JButton getButton = new JButton( "Get" );

      getButton.addActionListener(

         new ActionListener() {

            // get value for specific key
            public void actionPerformed( ActionEvent event )
            {
               Object value = table.get(  movieNameField.getText() );
              firstNameField.setText(value.toString());


               TestItemRecommend other = new TestItemRecommend(HashtableTest2.this);
                other.simple_2();

                String t = displayArea.getText();
                textField_1.setText(t);

                String c = textField_1.getText();




                //Display In messagedialog box what displayArea shows and Convert it from ID to Name

                JTextArea panel = new JTextArea();



                panel.setText(t);

                JScrollPane scrollPane = new JScrollPane(panel);

                JOptionPane.showMessageDialog(null, scrollPane,"Movies We Recommend", 0);


            }
         }
      );





      southPanel.add( getButton );

      textField_1 = new JTextField();
      southPanel.add(textField_1);
      textField_1.setColumns(10);

      // button to display hash table elements
      JButton listElementsButton = new JButton( "List objects" );

      listElementsButton.addActionListener(

         new ActionListener() {

            // display hash table elements
            public void actionPerformed( ActionEvent event )
            {

                 String d = textField_1.getText();

                   String e = table.get(d);
                if (e==null) e= "Nothing Found.";
                   textField.setText(e);

               StringBuffer buffer = new StringBuffer();

               for ( Enumeration<String> enumeration = table.elements();
                     enumeration.hasMoreElements(); )
                  buffer.append(
                     enumeration.nextElement() ).append( '\n' );

               displayArea.setText( buffer.toString() );


            }
         }
      );

      southPanel.add( listElementsButton );

and I'm getting no result.I found that the problem is in String e for which I get a null!If I manually set the value of textField_1,I get result,but when I'm asking to do this automatically,(by Code)it doesn't.What do you suggest? Thank you!

arctu
  • 1
  • 3
  • Debug it, e. g. add following line before the line where the NPE is thrown: System.out.println("table.get(" + t + ") returns " + table.get(t)); – howlger Jul 27 '17 at 13:28
  • I did as you said,so the recommendation that I get is 204,and system.out says:204 table.get(204) returns null – arctu Jul 27 '17 at 13:48
  • So *convert* is *null* and on *null* you can not call *toString()*. It looks you [swapped key and value](https://docs.oracle.com/javase/8/docs/api/java/util/Hashtable.html#put-K-V-). Add the following line to make it work: table.put("204", "Back To The Future"); – howlger Jul 27 '17 at 15:18
  • Where exactly to put this line?And why convert is null,since convert=table.get(t)? – arctu Jul 27 '17 at 15:47
  • Instead of the line: table.put("Back To The Future", 204); – howlger Jul 27 '17 at 15:50
  • The same result again.Not working! – arctu Jul 27 '17 at 15:53
  • Did you `table.put(204, ...` instead of `table.put("204", ...`? – howlger Jul 27 '17 at 15:57
  • No.I put ("204",.... – arctu Jul 27 '17 at 15:58
  • In `Object convert = table.get(t);` what values can have `t` and what should be in `convert`? – howlger Jul 27 '17 at 16:10
  • t are the values that JTextArea displayArea gets. – arctu Jul 27 '17 at 16:15
  • If I manually set value of table.get("204"),I get result.So the problem is in t,so in displayArea text,which comes from SystemOut – arctu Jul 27 '17 at 21:12
  • What should the user enter into the text field and what message should the dialog displayed when the user press the button? Currently, if the user enters for example *GoldenEye* and presses the *putButton* and then the *Get* button a dialog shows *2*. If the *putButton* has not been pressed before the *Get* button or if in the hash table no value can be found for the entered key, an exception is thrown. For these cases you have to add the line `if (convert == null) convert = "Nothing found.";` before the line `panel.setText(convert.toString());`. – howlger Jul 27 '17 at 22:59
  • See also _[What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/a/218510/6505250)_ – howlger Jul 28 '17 at 07:37
  • I edited the whole thing,please take a look again if you want.Thanks! – arctu Jul 28 '17 at 17:28
  • That looks better. :-) Check if _d_ contains the correct value and the Hashtable contains the correct mappings, e. g. if _d_ contains _foo_ and there was `table.put("foo", "bar")` then `table.get(d)` will be resolved to _bar_. – howlger Jul 28 '17 at 21:57

0 Answers0