d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Why Can It Not Find Symbol?
Add Reply New Topic New Poll
Member
Posts: 29,348
Joined: Mar 27 2008
Gold: 504.69
Dec 24 2015 09:49am
Just coding for fun. Working on some type of server/client.
Just wondering when I am trying to update the userArea panel why it cannot find the getInetAdress method even though I have used it already.

Full code
Code
/************************************
* Server *
************************************/
import java.io.*; //IOException
import java.net.*; //Socket, ServerSocket, InetAddress, UnknownHostException
import java.util.*; //ArrayList, Date
import java.awt.*; //JFrame
import javax.swing.*; //JTextArea, JScrollPane

public class Server extends JFrame{
private TextPanel textPanel;
private UserPanel userPanel;
private JTextField textField;

private ArrayList users = new ArrayList();

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

public Server(){
setLayout(new BorderLayout());

textPanel = new TextPanel();
add(textPanel, BorderLayout.CENTER);

userPanel = new UserPanel();
add(userPanel, BorderLayout.EAST);

textField = new JTextField();
add(textField, BorderLayout.SOUTH);

setTitle("Server");
setSize(700,500);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

try
{
ServerSocket serverSocket = new ServerSocket(8000); //attempt to create a server socket
textPanel.addToPanel("Server started at " + new Date() + "\n");
textPanel.addToPanel("Host Name/IP Address: " + InetAddress.getLocalHost() + "\n"); //get host address

while(true){
Socket socket = serverSocket.accept(); //listen for connection requests

textPanel.addToPanel(socket.getInetAddress() + " has connected.\n"); //get client ip

users.add(socket); //add client to the list
userPanel.updateUserList(users);

//Create data input and output streams
DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());
}
}
catch(IOException ex){
System.err.println(ex);
}
}
}
/************************************
* TextPanel *
************************************/
class TextPanel extends JPanel
{
private JTextArea textArea = new JTextArea();

public TextPanel()
{
setLayout(new BorderLayout());
textArea.setEditable(false);
add(new JScrollPane(textArea), BorderLayout.CENTER);
}

void addToPanel(String string)
{
textArea.append(string);
}
}
/************************************
* UserPanel *
************************************/
class UserPanel extends JPanel
{
private JTextArea userArea = new JTextArea();

public UserPanel()
{
setLayout(new BorderLayout());
setPreferredSize(new Dimension(200,475));
userArea.setEditable(false);
add(new JScrollPane(userArea), BorderLayout.CENTER);
}

void updateUserList(ArrayList arrayList)
{
for(int i = 0; i<arrayList.size(); i++)
{
userArea.append((arrayList.get(i)).getInetAddress());
}
}
}


Problem Area
Code
void updateUserList(ArrayList arrayList)
{
for(int i = 0; i<arrayList.size(); i++)
{
userArea.append((arrayList.get(i)).getInetAddress());
}
}


Error
Code
Server.java:101: error: cannot find symbol
userArea.append((arrayList.get(i)).getInetAddress());
^
symbol: method getInetAddress()

Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 24 2015 10:42am
you're using a non-generic arraylist. when you call arrayList.get(i), its reference type is Object, which doesn't have getInetAddress() defined. you have two options:

1) use generics. ArrayList<Socket> users = new ArrayList<Socket>();
2) cast it prior to calling your method: ((Socket)arrayList.get(i)).getInetAddress()

in java, generics actually converts option 1 into option 2, so there's no performance benefit. but if you use option 1, it can help catch bugs at compile time.
Member
Posts: 29,348
Joined: Mar 27 2008
Gold: 504.69
Dec 24 2015 10:59am
Quote (carteblanche @ Dec 24 2015 12:42pm)
you're using a non-generic arraylist. when you call arrayList.get(i), its reference type is Object, which doesn't have getInetAddress() defined. you have two options:

1) use generics. ArrayList<Socket> users = new ArrayList<Socket>();
2) cast it prior to calling your method: ((Socket)arrayList.get(i)).getInetAddress()

in java, generics actually converts option 1 into option 2, so there's no performance benefit. but if you use option 1, it can help catch bugs at compile time.


Thanks very much. Option 1 didn't seem to do anything, but option 2 worked without a problem.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 24 2015 11:28am
Quote (ROM @ Dec 24 2015 11:59am)
Thanks very much. Option 1 didn't seem to do anything, but option 2 worked without a problem.


sorry, i wasn't very clear. every time you declare your arraylist you need to use generics for #1. since you declared it as a parameter, you'd need to parameterize it as well.

void updateUserList(ArrayList<Socket> arrayList)
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll