control nxt with pc problems

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

control nxt with pc problems

Postby janvos2506 » Thu Jul 12, 2012 6:01 pm

hey i wanted to make a program to controls the net and this is my code

Code: Select all
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.swing.JFrame;

import lejos.pc.comm.NXTCommLogListener;
import lejos.pc.comm.NXTConnector;
 
/**
 * This is a PC sample. It connects to the NXT, and then
 * sends an integer and waits for a reply, 100 times.
 *
 * Compile this program with javac (not nxjc), and run it
 * with java.
 *
 * You need pccomm.jar on the CLASSPATH and the jfantom.dll
 * DLL or liblibnxt.so shared library on the Java library path.
 *
 * Run the program by:
 *
 *   java USBSend
 *
 * Your NXT should be running a sample such as USBReceive.
 *
 * @author Lawrie Griffiths
 *
 */
public class BlueToothClass implements KeyListener {
   
   public BlueToothClass() {
      JFrame Window = new JFrame("My first JFrame Window!");
        Window.setLocation(200,100);
        Window.setSize(800,600);
        Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Window.setVisible(true);

        Window.addKeyListener(this);
   }
   
   static NXTConnector conn = null;
   static DataInputStream inDat = null;
   static DataOutputStream outDat = null;
   
   public static void main(String[] args) {
      new BlueToothClass();
      conn = new NXTConnector();
      
      conn.addLogListener(new NXTCommLogListener(){

         public void logEvent(String message) {
            System.out.println("USBSend Log.listener: "+message);
            
         }

         public void logEvent(Throwable throwable) {
            System.out.println("USBSend Log.listener - stack trace: ");
             throwable.printStackTrace();
            
         }
         
      }
      );
      
      if (!conn.connectTo("usb://")){
         System.err.println("No NXT found using USB");
         System.exit(1);
      }
       inDat = new DataInputStream(conn.getInputStream());
       outDat = new DataOutputStream(conn.getOutputStream());      
   }

   @Override
   public void keyPressed(KeyEvent e) {
      if(e.getKeyCode() == KeyEvent.VK_W) {
          try {
            outDat.writeInt(50);
            outDat.flush();
         } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
         }
          }
      
   }

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

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


and the NXT
Code: Select all
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;

import lejos.nxt.LCD;
import lejos.nxt.comm.USB;
import lejos.nxt.comm.USBConnection;

/**
 * Test of Java streams over USB.
 * Run the PC example, USBSend, to send data.
 *
 * @author Lawrie Griffiths
 *
 */
public class USBReceive {

   public static void main(String [] args) throws Exception
   {
      LCD.drawString("waiting", 0, 0);
      USBConnection conn = USB.waitForConnection();
      DataOutputStream dOut = conn.openDataOutputStream();
      DataInputStream dIn = conn.openDataInputStream();
      
      while (true)
      {
            int b;
            try
            {
                b = dIn.readInt();
            }
            catch (EOFException e)
            {
                break;
            }         
         dOut.writeInt(-b);
         dOut.flush();
           LCD.drawInt(b,8,0,1);
      }
        dOut.close();
        dIn.close();
        conn.close();
   }
}



and i get these errors when i press the W button
Code: Select all
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
   at BlueToothClass.keyPressed(BlueToothClass.java:80)
   at java.awt.Component.processKeyEvent(Component.java:6333)
   at java.awt.Component.processEvent(Component.java:6152)
   at java.awt.Container.processEvent(Container.java:2083)
   at java.awt.Window.processEvent(Window.java:1835)
   at java.awt.Component.dispatchEventImpl(Component.java:4737)
   at java.awt.Container.dispatchEventImpl(Container.java:2141)
   at java.awt.Window.dispatchEventImpl(Window.java:2482)
   at java.awt.Component.dispatchEvent(Component.java:4565)
   at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1836)
   at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:712)
   at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:990)
   at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:855)
   at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:676)
   at java.awt.Component.dispatchEventImpl(Component.java:4609)
   at java.awt.Container.dispatchEventImpl(Container.java:2141)
   at java.awt.Window.dispatchEventImpl(Window.java:2482)
   at java.awt.Component.dispatchEvent(Component.java:4565)
   at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:684)
   at java.awt.EventQueue.access$000(EventQueue.java:85)
   at java.awt.EventQueue$1.run(EventQueue.java:643)
   at java.awt.EventQueue$1.run(EventQueue.java:641)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
   at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
   at java.awt.EventQueue$2.run(EventQueue.java:657)
   at java.awt.EventQueue$2.run(EventQueue.java:655)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
   at java.awt.EventQueue.dispatchEvent(EventQueue.java:654)
   at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
   at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
   at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
USBSend Log.listener: Failed to connect to the specified NXT
USBSend Log.listener: Failed to connect to any NXT
No NXT found using USB

the computer also doesn't find the nxt?? help?
janvos2506
Novice
 
Posts: 34
Joined: Mon Oct 24, 2011 8:58 pm

Re: control nxt with pc problems

Postby skoehler » Thu Jul 12, 2012 6:41 pm

Please learn how to program. As Java programm, you should know what a NullPointerException is and what it means. The problem is rather simple. And it's a simple programming mistake on your side.
skoehler
leJOS Team Member
 
Posts: 1431
Joined: Thu Oct 30, 2008 4:54 pm


Return to NXJ Software

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 3 guests

more stuff