Frage

I'm creating something with GUI and binary IO and I'm getting red lined here when using InputStream.

I have created a new Object, Buffered and File InputStream as in. when I use in.readUTF, in.readDouble, in.readObject why am I getting red lined?

Thanks in advance!

public RecordViewerPanel() throws IOException {
    initComponents();
    try{ObjectInputStream in = new ObjectInputStream(
                              new BufferedInputStream(
                              new FileInputStream(filename)));
    } catch (FileNotFoundException ex) {
        Logger.getLogger(RecordViewerPanel.class.getName()).log(Level.SEVERE, null, ex);
    }   
}

private void refreshUI() {
    SalesAgent sale = (customer.get(curIndex));
    firstTextField.setText(sale.getFirst());
    lastTextField.setText(sale.getLast());
    salesTextField.setText("$" + String.valueOf(sale.getSales()));
    dateTextField.setText(String.valueOf(sale.getDate()));
    recordPanel.repaint();
}

ArrayList<SalesAgent> salesForce = new ArrayList<SalesAgent>();
String first = in.readUTF();
String last = in.redUTF();
double sales = in.readDouble();
Date date = (Date)(in.readObject());
War es hilfreich?

Lösung

in is declared within RecordViewerPanel(). Its scope is within the {} for the try/catch block. Hence in the following line:

String first = in.readUTF();

in is not something that is recognized by Java which is the reason for the error message Cannot find variable in.

You can fix this problem by passing in around or by declaring it such that it is within the scope of where it is being called or used.

Also fix the following line:

String last = in.redUTF();

to

String last = in.readUTF();

Again though, make sure in is in scope.

Andere Tipps

Quick look, but isn't this:

String last = in.redUTF();

Supposed to be this:

String last = in.readUTF();

?

Your problem is actually two-fold here, you've got your 'in' variable declared inside of a try/catch block, which means it is not accessible further down (where you try to make use of it). Actually, it's also declared within the scope of another method, which means it could never be visible to that block below. The code you've pasted doesn't actually look valid, as you can't have function declarations inside of other functions in Java (with the exception of anonymous classes). Additionally, you've got a typo with your 'last' variable, should be in.readUTF(); instead of redUTF().

Your input is declared inside of the try catch and youre accessing it outside of that scope.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top