2010-05-28 94 views
0

直升機,有文件,CustomerClient.javaCustomerServer.javaCustomer.javaJava編程訪問對象變量


問題:CustomerServer。 java文件,當我編譯CustomerServer.java時出現錯誤:System.out.pri ntln(A [k]的.getName());


ERROR:


init: 

deps-jar: 

Compiling 1 source file to C:\Documents and Settings\TLNA\My Documents\NetBeansProjects\Server\build\classes 

C:\Documents and Settings\TLNA\My Documents\NetBeansProjects\Server\src\CustomerServer.java:44: cannot find symbol 

symbol : method getName() 

location: class Customer 

         System.out.println(a[k].getName()); 
1 error 
BUILD FAILED (total time: 0 seconds) 


CustomerClient.java


import java.io.*; 

import java.net.*; 

import java.awt.*; 

import java.awt.event.*; 

import javax.swing.*; 

import javax.swing.border.*; 


public class CustomerClient extends JApplet { 

private JTextField jtfName = new JTextField(32); 
private JTextField jtfSeatNo = new JTextField(32); 
// Button for sending a student to the server 
private JButton jbtRegister = new JButton("Register to the Server"); 
// Indicate if it runs as application 
private boolean isStandAlone = false; 
// Host name or ip 
String host = "localhost"; 

public void init() { 


    JPanel p1 = new JPanel(); 
    p1.setLayout(new GridLayout(2, 1)); 
    p1.add(new JLabel("Name")); 
    p1.add(jtfName); 
    p1.add(new JLabel("Seat No.")); 
    p1.add(jtfSeatNo); 

    add(p1, BorderLayout.CENTER); 
    add(jbtRegister, BorderLayout.SOUTH); 


    // Register listener 
    jbtRegister.addActionListener(new ButtonListener()); 

    // Find the IP address of the Web server 
    if (!isStandAlone) { 
     host = getCodeBase().getHost(); 
    } 
} 

/** Handle button action */ 
private class ButtonListener implements ActionListener { 

    public void actionPerformed(ActionEvent e) { 
     try { 
      // Establish connection with the server 
      Socket socket = new Socket(host, 8000); 

      // Create an output stream to the server 
      ObjectOutputStream toServer = 
        new ObjectOutputStream(socket.getOutputStream()); 

      // Get text field 
      String name = jtfName.getText().trim(); 
      String seatNo = jtfSeatNo.getText().trim(); 

      // Create a Student object and send to the server 
      Customer s = new Customer(name, seatNo); 
      toServer.writeObject(s); 
     } catch (IOException ex) { 
      System.err.println(ex); 
     } 
    } 
} 

/** Run the applet as an application */ 
public static void main(String[] args) { 
    // Create a frame 
    JFrame frame = new JFrame("Register Student Client"); 

    // Create an instance of the applet 
    CustomerClient applet = new CustomerClient(); 
    applet.isStandAlone = true; 

    // Get host 
    if (args.length == 1) { 
     applet.host = args[0]; 

    // Add the applet instance to the frame 
    } 
    frame.add(applet, BorderLayout.CENTER); 

    // Invoke init() and start() 
    applet.init(); 
    applet.start(); 

    // Display the frame 
    frame.pack(); 
    frame.setVisible(true); 
} 
} 

CustomerServer.java


import java.io.*; 
import java.net.*; 

public class CustomerServer { 

private String name; 
private int i; 
private ObjectOutputStream outputToFile; 
private ObjectInputStream inputFromClient; 

public static void main(String[] args) { 
    new CustomerServer(); 

} 

public CustomerServer() { 
    Customer[] a = new Customer[30]; 
    try { 
     // Create a server socket 
     ServerSocket serverSocket = new ServerSocket(8000); 
     System.out.println("Server started "); 

     // Create an object ouput stream 
     outputToFile = new ObjectOutputStream(
       new FileOutputStream("student.dat", true)); 

     while (true) { 
      // Listen for a new connection request 
      Socket socket = serverSocket.accept(); 

      // Create an input stream from the socket 
      inputFromClient = 
        new ObjectInputStream(socket.getInputStream()); 

      // Read from input 
      //Object object = inputFromClient.readObject(); 
      for (int k = 0; k <= 2; k++) { 
       if (a[k] == null) { 
        a[k] = (Customer) inputFromClient.readObject(); 
        // Write to the file 
        outputToFile.writeObject(a[k]); 
        //System.out.println("A new student object is stored"); 

    System.out.println(a[k].getName()); 

    break; 
       } 

       if (k == 2) { 
        //fully booked 
        outputToFile.writeObject("All seats are booked"); 
        break; 
       } 
      } 



     } 
    } catch (ClassNotFoundException ex) { 
     ex.printStackTrace(); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } finally { 
     try { 
      inputFromClient.close(); 
      outputToFile.close(); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 
} 
} 

Customer.java


public class Customer implements java.io.Serializable { 

private String name; 
private String seatno; 

public Customer(String name, String seatno) { 
    this.name = name; 

    this.seatno = seatno; 
} 

public String getName() { 
    return name; 
} 

public String getSeatNo() { 
    return seatno; 
} 
} 
+1

您確定客戶已正確重新編譯?你嘗試過一個乾淨的構建? – 2010-05-28 19:39:18

+0

不應該將構建工具(Ant?Maven?)也作爲標籤包含在內? – Powerlord 2010-05-28 19:41:09

+0

@R。 Bemrose - 不,因爲這個問題不是關於構建工具的。 – danben 2010-05-28 19:59:21

回答

5

構建消息說,它只是一個編譯源文件。也許Customer類更改爲包含getName函數,並且自此以後未重新編譯。

您是否嘗試同時編譯所有三個源文件?

1

重新編譯Customer.java並確保您沒有類文件的重複版本。使用調試器(在客戶類反序列化之後設置斷點)以進一步調試。