Java programming accessing object variables
Posted
by Haxed
on Stack Overflow
See other posts from Stack Overflow
or by Haxed
Published on 2010-05-28T19:34:03Z
Indexed on
2010/05/28
19:41 UTC
Read the original article
Hit count: 228
Helo, there are 3 files, CustomerClient.java, CustomerServer.java and Customer.java
PROBLEM: In the CustomerServer.java file, i get an error when I compile the CustomerServer.java at line : System.out.println(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;
}
}
© Stack Overflow or respective owner