简体   繁体   中英

Only able to call some methods of a separate class in the same package in my main method

I am trying to create an instance of the BrowserSession class in my main method and for some reason NetBeans cannot access the set methods or the constructor when it is passing a Socket as a parameter. Both my YK250X and BrowserSession are in the same package and I am able to create an instance of BrowserSession as long as I am not trying to pass anything. Otherwise I get a cannot find symbol error message.

Here is my main method code:

package yk250x;
/**
 *
 * @author Sophia_M-D
 */
import java.net.ServerSocket;
import java.net.Socket;
import java.io.BufferedReader;

public class YK250X {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        int BrowserListenPort = 100;

        ServerSocket BrowserListenSocket = new ServerSocket(BrowserListenPort);
        Socket BrowserConnectionSocket = BrowserListenSocket.accept();
        BrowserSession ThisBrowserSession = new BrowserSession();
        ThisBrowserSession.setBrowserConnectionSocket(BrowserConnectionSocket);
    }
}

And here is my BrowserSession class code:

/**
 ** @author srm349
 */
import java.net.ServerSocket;
import java.net.Socket;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;
import java.lang.Thread;

public class BrowserSession{

    //Member Variables
    private Socket BrowserConnectionSocket;
    private PrintWriter BrowserStream;

    //Constructor Methods
    public BrowserSession(Socket NewSocket){
        BrowserConnectionSocket = NewSocket;
    }

    public BrowserSession(){
    }

    //Get&Set Methods
    public Socket getBrowserConnectionSocket(){
        return BrowserConnectionSocket;
    }

    public void setBrowserConnectionSocket(Socket newConnection){
        BrowserConnectionSocket = newConnection;
    }

    // method through which information is sent to the browser
    public boolean WriteMessage(String Message){
        try{
            if(BrowserStream == null)
                BrowserStream = new PrintWriter(BrowserConnectionSocket.getOutputStream());
            BrowserStream.println(Message);
            BrowserStream.flush();
            return true;
        } catch(IOException Error) {
            return false;
        }
    }
}

I do not use NetBeans, but you may have to include package yk250x; at the top of your BrowserSession file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM