Hello,
We are trying to develop a chat client program in a computer science class, but we are running into this error
Invalid reference in inner class to a non-final local variable
. The chat client will send the users' text to a text file and then read depending on a session ID.
This is the exact error: Invalid reference in inner class "Irc$1" to a non-final local variable, "jt", declared in method "main".
I will take you through what we are trying to achive. Here are the specific spots in our code that we are looking at:
1. Starts with the input text area.
// Input text area
JTextArea jt = new JTextArea (7, 50);
frame.getContentPane ().add (panel);
jt.setLineWrap (true);
jt.setWrapStyleWord (true);
JScrollPane sbtText;
sbtText = new JScrollPane (jt);
sbtText.setVerticalScrollBarPolicy (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
panel.add (sbtText);
2. User hits send button and the text in the textarea is converted into a string and sent to the sendString method.
// Send Button
JButton btnSend; // Send Message
btnSend = new JButton ("SEND");
btnSend.addActionListener (
new ActionListener ()
{
public void actionPerformed (ActionEvent e)
{
String s = jt.getText ();
if (!s.equals (""))
{
jt.selectAll ();
// Send the string
sendString (s);
}
}
}
);
panel.add (btnSend);
3. The sendString method uses a class called writer to write the text to a text file. This will then be read and outputed into the output textarea later. (We haven't gotten that far :p)
private static void sendString (String s)
{
Writer w;
w = new Writer (ta);
w.Writer (jt, session);
}
4. I tried putting this in too, just to see if it would do anything.
// Various GUI components and info
public static JFrame frame = null;
public final static JTextArea jt = null;
public final static JTextArea ta = null;
Here is the entire code:
// The "Irc" class.
import java.awt.*;
import java.awt.event.*;
import hsa.Console;
import java.util.Date;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import javax.swing.JTextArea;
import javax.swing.text.JTextComponent;
public class Irc
{
// Various GUI components and info
public static JFrame frame = null;
public final static JTextArea jt = null;
public final static JTextArea ta = null;
static Console c; // The output console
public static void main (String[] args) throws IOException
{
c = new Console (40, 100);
int session;
String output = "This is the test output";
// Jframe title
JFrame frame = new JFrame ("Chat Client");
// Output text area
String text = "TEST TEXT A JTextArea object represents a multiline area for displaying text. "
+ "You can change the number of lines that can be displayed at a time, "
+ "as well as the number of columns. You can wrap lines and words too. "
+ "You can also put your JTextArea in a JScrollPane to make it scrollable.dsgdsg"
+ "sdgsdgggggggggggggggggggggggggggggggg";
// Declare a new JPanel to put the text boxes on
JPanel panel = new JPanel ();
//Output text area
// Create a scrollable text area
JTextArea ta = new JTextArea (text, 15, 50);
ta.setLineWrap (true);
JScrollPane sbrText;
sbrText = new JScrollPane (ta);
sbrText.setVerticalScrollBarPolicy (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
panel.add (sbrText);
// Input text area
JTextArea jt = new JTextArea (7, 50);
frame.getContentPane ().add (panel);
jt.setLineWrap (true);
jt.setWrapStyleWord (true);
JScrollPane sbtText;
sbtText = new JScrollPane (jt);
sbtText.setVerticalScrollBarPolicy (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
panel.add (sbtText);
// Send Button
JButton btnSend; // Send Message
btnSend = new JButton ("SEND");
btnSend.addActionListener (
new ActionListener ()
{
public void actionPerformed (ActionEvent e)
{
String s = jt.getText ();
if (!s.equals (""))
{
jt.selectAll ();
// Send the string
sendString (s);
}
}
}
);
panel.add (btnSend);
// Jframe toolbar (text input tools)
JToolBar toolbar = new JToolBar ("Toolbar", JToolBar.HORIZONTAL);
frame.getContentPane ().add (toolbar, BorderLayout.NORTH);
// BOLD BUTTON
JButton boldbutton = new JButton (new ImageIcon ("text-bold-icon.png"));
toolbar.add (boldbutton);
boldbutton.addActionListener (
new ActionListener ()
{
public void actionPerformed (ActionEvent e)
{
//setSelectionColor(Color.green);
}
}
);
// ITALIC BUTTON
JButton italicbutton = new JButton (new ImageIcon ("text-italic-icon.png"));
toolbar.add (italicbutton);
// UNDERLINE BUTTON
JButton underlinebutton = new JButton (new ImageIcon ("text-underline-icon.png"));
toolbar.add (underlinebutton);
// FONT BUTTON
JButton fontbutton = new JButton (new ImageIcon ("font-icon.png"));
toolbar.add (fontbutton);
// COLOR BUTTON
JButton colorbutton = new JButton (new ImageIcon ("color-wheel-icon.png"));
toolbar.add (colorbutton);
// LINK BUTTON
JButton linkbutton = new JButton (new ImageIcon ("page-white-link-icon.png"));
toolbar.add (linkbutton);
// UPLOAD BUTTON
JButton uploadbutton = new JButton (new ImageIcon ("page-white-get-icon.png"));
toolbar.add (uploadbutton);
// SAVE BUTTON
JButton savebutton = new JButton (new ImageIcon ("disk-icon.png"));
toolbar.add (savebutton);
// Jframe settings
frame.setUndecorated (true);
frame.getRootPane ().setWindowDecorationStyle (JRootPane.PLAIN_DIALOG);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setSize (600, 500);
frame.setVisible (true);
} // main method
private static void sendString (String s)
{
Writer w;
w = new Writer (ta);
w.Writer (jt, session);
}
public static boolean login (String Login) throws IOException
{
String lineDump; //to read file
BufferedReader input = new BufferedReader (new FileReader ("login.txt"));
int i = 0;
boolean passcheck = false; //to return
while (i == 0)
{
lineDump = input.readLine ();
if (lineDump == null)
{
passcheck = false;
break;
}
if (Login.equals (lineDump))
{
passcheck = true;
break;
}
}
return passcheck;
}
} // Irc class