sorry i'm nob in java programming,
i'm high schooler who want learn java, but i have no teacher, please guide me , , ,
Here is the Client from book, then this code called using another script,,, connect -> openFile -> copyFile -> closeFile;import java.io.*; import java.net.*; import com.deitel.ch17.AccountRecordSerializable; public class ClientRemCopySeqFile{ private ObjectInputStream input; private ObjectOutputStream socketOut; private Socket client; String host = "localhost"; // enable user to select file to open public void openFile() { try // open file { input = new ObjectInputStream( new FileInputStream( "clients.ser" ) ); } // end try catch ( IOException ioException ) { System.err.println( "Error opening file." ); } // end catch } // end method openFile public void connect() throws Exception { BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); DatagramSocket clientSocket = new DatagramSocket(); InetAddress IPAddress = InetAddress.getByName("localhost"); byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024]; DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); clientSocket.send(sendPacket); } public void copyFile() { AccountRecordSerializable record; try // input the values from the file { while ( true ) { record = ( AccountRecordSerializable ) input.readObject(); // display record contents System.out.printf( "%-10d%-12s%-12s%10.2f\n", record.getAccount(), record.getFirstName(), record.getLastName(), record.getBalance() ); socketOut.writeObject( record ); // output record } // end while } // end try catch ( EOFException endOfFileException ) { System.out.println("Records has been sent..."); return; // end of file was reached } // end catch catch ( ClassNotFoundException cnf ) { System.err.println( "Unable to create object."+cnf.toString() ); } // end catch catch ( IOException ioException ) { System.err.println( "Error during reading from file." ); } // end catch// end catch } // close file and terminate application public void closeFile() { try // close file and exit { if ( input != null ) input.close(); if ( client != null ) client.close(); if ( socketOut != null ) socketOut.close(); System.out.println("connection for "+host+" has been terminated ..."); System.exit( 0 ); } // end try catch ( IOException ioException ) { System.err.println( "Error closing file." ); System.exit( 1 ); } // end catch } // end method closeFile } Here the Server from the book too, called using another program, connect -> openFile -> listen -> copyFile -> closeFile import java.io.*; import java.net.*; import com.deitel.ch17.AccountRecordSerializable; public class ServerRemCopySeqFile{ private ServerSocket server = null; private Socket client=null; private ObjectInputStream streamIn; private ObjectOutputStream output; public void connect() throws Exception { DatagramSocket serverSocket = new DatagramSocket(9876); byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; while(true) { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); String sentence = new String( receivePacket.getData()); InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); } } public void listen(){ try { client = server.accept(); } catch (IOException e) { System.out.println("Did not accept connection: " + e); System.exit(1); } System.out.println("Client connection accepted. Moving to local port ..."); } // enable user to select file to open public void openFile(){ try // open file { output = new ObjectOutputStream( new FileOutputStream( "remclients.ser" ) ); } // end try catch ( IOException ioException ) { System.err.println( "Error opening file." ); } // end catch } // end method openFile public void copyFile(){ AccountRecordSerializable record; try // input the values from the file { streamIn = new ObjectInputStream(new BufferedInputStream(client.getInputStream())); while ( true ) { record = ( AccountRecordSerializable ) streamIn.readObject(); // display record contents System.out.printf( "%-10d%-12s%-12s%10.2f\n", record.getAccount(), record.getFirstName(), record.getLastName(), record.getBalance() ); output.writeObject( record ); // output record } // end while } // end try catch ( EOFException endOfFileException ) { System.out.println("Records has been save..."); return; // end of file was reached } // end catch catch ( ClassNotFoundException classNotFoundException ) { System.err.println( "Unable to create object." ); } // end catch catch ( IOException ioException ) { System.err.println( "Error during reading from file." ); } // end catch// end catch } // close file and terminate application public void closeFile() { try // close file and exit { if ( server != null ) server.close(); if ( client != null ) client.close(); if ( streamIn != null ) streamIn.close(); System.out.println("All connections and file has been closed ..."); System.exit( 0 ); } // end try catch ( IOException ioException ) { System.err.println( "Error closing file." ); System.exit( 1 ); } // end catch } // end method closeFile }
Please help me, after run I got error message
Exception in thread "main" java.lang.NullPointerException
at ClientRemCopySeqFile.copyFile(ClientRemCopySeqFile .java:51)
at ClientRemCopySeqFileTest.main(ClientRemCopySeqFile Test.java:13)