Results 1 to 6 of 6
  1. #1
    psychop is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default HELP - get this error and don't know why!

    Made 2 separate progs so I could test if all works. When combined them, the first part when it gets current mouse position works great, but when clicking START button get this error:

    Exception in thread "Thread-3" java.lang.NullPointerException
    at farm.mouseMoveAndClick(farm.java:442)
    at farm$HandleEvent.run(farm.java:142)

    This is the code and in BOLD are the problems:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.MouseInfo;
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.Dimension;
    import javax.swing.*;
    
    public class farm extends JFrame
            implements KeyListener,
            ActionListener
    {
        Robot robot;
        int osnX;
        int osnY;
        JTextArea displayArea;
        JTextField typingArea;
        static final String newline = System.getProperty("line.separator");
        
        public static void main(String[] args) {
            new farm();
            /* Use an appropriate Look and Feel */
            try {
                //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                //UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
                UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            } catch (UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            } catch (InstantiationException ex) {
                ex.printStackTrace();
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
            /* Turn off metal's use of bold fonts */
            UIManager.put("swing.boldMetal", Boolean.FALSE);
            
            //Schedule a job for event dispatch thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
     
      public farm(){
        try
        {
        	    robot = new Robot();
        }
        catch(AWTException e)
        {
        	    e.printStackTrace();
        }
      }
      
      
    //Create the GUI and show it.  For thread safety,
        private static void createAndShowGUI() {
            //Create and set up the window.
            farm frame = new farm("Farmville klikanje");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            //Set up the content pane.
            frame.addComponentsToPane();
            
            //Display the window.
            frame.pack();
            frame.setVisible(true);
            frame.setAlwaysOnTop(true);
        }
        
        private void addComponentsToPane() {
            
            JButton button = new JButton("Start");
            button.addActionListener(this);
            
            typingArea = new JTextField(20);
            typingArea.addKeyListener(this);
            
            displayArea = new JTextArea();
            displayArea.setEditable(false);
            JScrollPane scrollPane = new JScrollPane(displayArea);
            scrollPane.setPreferredSize(new Dimension(375, 125));
            
            getContentPane().add(typingArea, BorderLayout.PAGE_START);
            getContentPane().add(scrollPane, BorderLayout.CENTER);
            getContentPane().add(button, BorderLayout.PAGE_END);
        }
        
        public farm(String name) {
            super(name);
        }
        
        
        /** Handle the key typed event from the text field. */
        public void keyTyped(KeyEvent e) {
    	osnX = MouseInfo.getPointerInfo().getLocation().x;
      	osnY = MouseInfo.getPointerInfo().getLocation().y;
            displayInfo(e, "Izbrali ste nulto točko. Pritisnite start za začetek!");
            displayInfo(e, osnX + ", " + osnY);
        }
        
        /** Handle the key pressed event from the text field. */
        public void keyPressed(KeyEvent e) {
            //displayInfo(e, "KEY PRESSED: ");
        }
        
        /** Handle the key released event from the text field. */
        public void keyReleased(KeyEvent e) {
            //displayInfo(e, "KEY RELEASED: ");
        }
        
        /** Handle the button click. */
        public void actionPerformed(ActionEvent e) {
            //Clear the text components.
            //displayArea.setText("");
            //typingArea.setText("");
            
            //Return the focus to the typing area.
            //typingArea.requestFocusInWindow();
    	   JButton button = (JButton)(e.getSource());
    	   new HandleEvent(button,this).start();
        }
    	 
        [B][COLOR="DarkRed"]class HandleEvent extends Thread[/COLOR][/B]
        {
        	    JButton button;
        	    ActionListener listener;
        	    HandleEvent(JButton button,ActionListener listener)
        	    {
        	    	    this.button = button;
        	    	    this.listener = listener;
        	    }
        	    public void run()
        	    {
        	    	    //Get the button's location.
        	    	    Point location = button.getLocationOnScreen();
        	    	    button.removeActionListener(listener);
        	    	    mouseMoveAndClick(osnX,osnY);
        	    	    //Re-enable action events on this button
        	    	    //button.addActionListener(listener);
        	    }
    
         }//end class HandleEvent
    
    // check all key events
         private void displayInfo(KeyEvent e, String keyStatus){
            
            //You should only rely on the key char if the event
            //is a key typed event.
            int id = e.getID();
            String keyString;
            if (id == KeyEvent.KEY_TYPED) {
                char c = e.getKeyChar();
                keyString = "key character = '" + c + "'";
            } else {
                int keyCode = e.getKeyCode();
                keyString = "key code = " + keyCode
                        + " ("
                        + KeyEvent.getKeyText(keyCode)
                        + ")";
            }
            
            int modifiersEx = e.getModifiersEx();
            String modString = "extended modifiers = " + modifiersEx;
            String tmpString = KeyEvent.getModifiersExText(modifiersEx);
            if (tmpString.length() > 0) {
                modString += " (" + tmpString + ")";
            } else {
                modString += " (no extended modifiers)";
            }
            
            String actionString = "action key? ";
            if (e.isActionKey()) {
                actionString += "YES";
            } else {
                actionString += "NO";
            }
            
            String locationString = "key location: ";
            int location = e.getKeyLocation();
            if (location == KeyEvent.KEY_LOCATION_STANDARD) {
                locationString += "standard";
            } else if (location == KeyEvent.KEY_LOCATION_LEFT) {
                locationString += "left";
            } else if (location == KeyEvent.KEY_LOCATION_RIGHT) {
                locationString += "right";
            } else if (location == KeyEvent.KEY_LOCATION_NUMPAD) {
                locationString += "numpad";
            } else { // (location == KeyEvent.KEY_LOCATION_UNKNOWN)
                locationString += "unknown";
            }
            
            displayArea.append(keyStatus + newline);
    /*        displayArea.append(keyStatus + newline
                   + "    " + keyString + newline
                    + "    " + modString + newline
                    + "    " + actionString + newline
                    + "    " + locationString + newline);*/
            displayArea.setCaretPosition(displayArea.getDocument().getLength());
        }
        
      [B][COLOR="DarkRed"]public void mouseMoveAndClick(int xStart,int yStart)[/COLOR][/B]
      {
        int i;
        int j;
        int mX=0;
        int mY=0;
        int novaX;
        int novaY;
        int kjeX;
        int kjeY;
        for ( j=0; j<32; j++ )
        {
    	    novaX = xStart - (23 * j);
    	    novaY = yStart - (9 * j);
    	    for ( i=0; i<40; i++ ) 
    	    {
    		    mX = novaX + (10 * i);
    		    mY = novaY - (5 * i);
    		    robot.mouseMove(mX,mY);
    		    robot.delay(50);
    		    robot.mousePress(InputEvent.BUTTON1_MASK);
    		    robot.delay(10);
    		    robot.mouseRelease(InputEvent.BUTTON1_MASK);
    		    kjeX = MouseInfo.getPointerInfo().getLocation().x;
    		    kjeY = MouseInfo.getPointerInfo().getLocation().y;
    		    if ((kjeX != mX) || (kjeY != mY))
    		    {
    			    System.exit( 0 );
    		    }
    	    }
        }
      }//end mouseMoveAndClick
    }

  2. #2
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    Rewrite the program so there is only one constructor for farm.
    As it is, the first constructor for farm creates a robot,
    but the second does not. And it is the second that is used by the GUI.
    The null exception comes from robot.mouseMove(..)

    Using a delay inside mouseMoveAndClick is also broken. Avoid Robot altogether.
    Please read and understand the tutorial How to Use Swing Timers

    In the future, please understand that the line number in a traceback is important.
    The code has highlit the headers of the items on the traceback,
    But the offending line is line 442, as shown in at farm.mouseMoveAndClick(farm.java:442)
    So line 442 should have been the one highlit.
    Last edited by zweibieren; 01-24-2010 at 05:42 PM. Reason: add note about line numbers

  3. #3
    psychop is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default

    Understand only few things tof what u say Z. problem is thet java is a bit diff than php and flash programing that am used 2. problem is that probably don't have proper progs for java programing that would have a debugegr so I could trace line by line and understand completly what is happening. So when these two "progs" are separated they both work fine but don't know how to fix this :(

  4. #4
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    The program has two constuctors for farm:
    Java Code:
        public farm(){
          try
          {
        	    robot = new Robot();
          }
          catch(AWTException e)
          {
          	    e.printStackTrace();
          }
        }
    
        
        public farm(String name) {
            super(name);
        }
    Eliminate the first and put its contents into the second.

  5. #5
    psychop is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default

    Yeah see that now what u ment. Fixed it and it worked. geez such smal mistake and such big problem. heeheh tnx

  6. #6
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    -----
    Using a delay inside mouseMoveAndClick is also broken. Avoid Robot altogether.
    Please read and understand the tutorial How to Use Swing Timers
    -----

Similar Threads

  1. Replies: 1
    Last Post: 12-05-2009, 08:28 AM
  2. Replies: 1
    Last Post: 10-26-2009, 05:38 PM
  3. Replies: 3
    Last Post: 01-23-2009, 09:53 AM
  4. error 530 error authentication required
    By rgale in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-12-2008, 05:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •