I am making a stock market simulator and I keep getting an error from each of my 3 classes the first class MYOSM the error is:
"Cannot find symbol- class stock"
The second class MarketTable outputs:
Cannot find MarketDataModel
And the third class MarketDataModel outputs the error:
Cannot find symbol- class stock
Have I gone wrong somewhere in my code I have checked it multiple times and I can't seem to locate it.
Here is my code:
MYOSM class:
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class MYOSM extends JFrame implements Runnable {
Stock[] market = {
new Stock("JTree", 14.57),
new Stock("JTable", 17.44),
new Stock("JList", 16.44),
new Stock("JButton", 7.21),
new Stock("JComponent", 27.40)
};
boolean monitor;
Random rg = new Random();
Thread runner;
public MYOSM() {
// Not meant to be shown as a real frame
super("Thread only version . . .");
runner = new Thread(this);
runner.start();
}
public MYOSM(boolean monitorOn) {
super("Stock Market Monitor");
setSize(400, 100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
monitor = monitorOn;
getContentPane().add(new JLabel("Trading is active. " +
"Close this window to close the market."),
BorderLayout.CENTER);
runner = new Thread(this);
runner.start();
}
public void run() {
while(true) {
int whichStock = Math.abs(rg.nextInt()) % market.length;
double delta = rg.nextDouble() - 0.4;
market[whichStock].update(delta);
if (monitor) {
market[whichStock].print();
}
try {
Thread.sleep(1000);
}
catch(InterruptedException ie) {
}
}
}
public Stock getQuote(int index) {
return market[index];
}
// This method returns the list of all the symbols in the market table
public String[] getSymbols() {
String[] symbols = new String[market.length];
for (int i = 0; i < market.length; i++) {
symbols[i] = market[i].symbol;
}
return symbols;
}
public static void main(String args[]) {
MYOSM myMarket = new MYOSM(args.length > 0);
myMarket.setVisible(true);
}
}
MarketTable class:
import java.awt.*;
import javax.swing.*;
public class MarketTable extends JFrame {
public MarketTable() {
super("Dynamic Data Test");
setSize(300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
MarketDataModel mdm = new MarketDataModel(5);
mdm.setStocks(new int[] { 0, 1, 2 });
JTable jt = new JTable(mdm);
JScrollPane jsp = new JScrollPane(jt);
getContentPane().add(jsp, BorderLayout.CENTER);
}
public static void main(String args[]) {
MarketTable mt = new MarketTable();
mt.setVisible(true);
}
}
MarketDataModel class:
import javax.swing.table.*;
import javax.swing.*;
public class MarketDataModel extends AbstractTableModel
implements Runnable {
Thread runner;
MYOSM market;
int delay;
public MarketDataModel(int initialDelay) {
market = new MYOSM();
delay = initialDelay * 1000;
Thread runner = new Thread(this);
runner.start();
}
Stock[] stocks = new Stock[0];
int[] stockIndices = new int[0];
String[] headers = {"Symbol", "Price", "Change", "Last updated"};
public int getRowCount() { return stocks.length; }
public int getColumnCount() { return headers.length; }
public String getColumnName(int c) { return headers[c]; }
public Object getValueAt(int r, int c) {
switch(c) {
case 0:
return stocks[r].symbol;
case 1:
return new Double(stocks[r].price);
case 2:
return new Double(stocks[r].delta);
case 3:
return stocks[r].lastUpdate;
}
throw new IllegalArgumentException("Bad cell (" + r + ", " + c +")");
}
public void setDelay(int seconds) { delay = seconds * 1000; }
public void setStocks(int[] indices) {
stockIndices = indices;
updateStocks();
fireTableDataChanged();
}
public void updateStocks() {
stocks = new Stock[stockIndices.length];
for (int i = 0; i < stocks.length; i++) {
stocks[i] = market.getQuote(stockIndices[i]);
}
}
public void run() {
while(true) {
updateStocks();
fireTableRowsUpdated(0, stocks.length - 1);
try { Thread.sleep(delay); }
catch(InterruptedException ie) {}
}
}
}
You're missing a Stock class there. Should be something like this I suppose:
public class Stock {
public Stock(String string, double d) {
this.symbol = string;
this.price = d;
}
public String symbol;
public double price;
public double delta;
public String lastUpdate;
public void print() {
System.out.println(this);
}
public void update(double delta2) {
this.delta = delta2;
}
#Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Stock [symbol=").append(symbol).append(", price=").append(price).append(", delta=").append(delta).append(", lastUpdate=")
.append(lastUpdate).append("]");
return builder.toString();
}
}
Or is it just in a different package than those classes?
Also it's weird your error message has "stock" in lowercase.
With the Stock class added as above, I have managed to start your code, but I'm not sure what it was supposed to be doing. I must note it is poorly written in general, with some basic mistakes like usage of default package.
Related
I'm in the process of making a GUI where I input a string into a text box and, once I click a Jbutton, a second text box will produce the string I inputted into the first or produce a random string from a method I created (public void associate()). When I run the GUI and click the button to produce the text in the second text box, everything works fine. However, when I click the button a second time so the GUI performs the same action, nothing happens. Is there anything I can do so that I don't have to close the GUI every time I wish to run it multiple times?
public class GUIWindow extends JFrame {
private Joketeller robot= new Joketeller();
private JLabel speakerlabel = new JLabel("Joke");
private JLabel MarcoLabel= new JLabel ("Marco");
private JTextField speakerfield= new JTextField ("Enter Joke Here");
private JTextField Marcofield= new JTextField ("",20);
private JButton Jokebutton=new JButton("Recite Joke >>>");
public GUIWindow() {
JPanel dataPanel= new JPanel(new GridLayout(2,2,12,16));
dataPanel.add(speakerlabel);
dataPanel.add(MarcoLabel);
dataPanel.add(speakerfield);
dataPanel.add(Marcofield);
JPanel buttonPanel= new JPanel();
buttonPanel.add(Jokebutton);
Container container = getContentPane();
container.add(dataPanel,BorderLayout.CENTER);
container.add(buttonPanel,BorderLayout.SOUTH);
Jokebutton.addActionListener(new JokeListener());
}
private class JokeListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String input=speakerfield.getText();
if (Jokebutton.isEnabled()) {
robot.setJoke(input);
String Response= robot.getResponse();
Marcofield.setText(Response);}
Joketeller class:
public class Joketeller {
private static String Marco;
private static String Response;
static int i= (int)(Math.random()*((5-1)+1)+1);
static String r;
public void setMarco(String Joke ) {
Marco=Joke;
}
public void setJoke(String Joke) {
Marco=Joke;
associate();
}
public String getJoke() {
return Marco;
}
public static String getMarco() {
return Marco;
}
public static void associate(){
if(i==1)
r= "Connect Angie";
else if(i==2)
r= "*Cloud Laugh*";
else if(i==3)
r= "Community";
else if(i==4)
r=getMarco();
else if(i==5)
r= "Indeed!";
Response=r;
}
public String getResponse() {
return Response;
}
}
Any help is appreciated. Thank you.
The first thing that jumps out at me, is the overuse of static...
public class Joketeller {
private static String Marco;
private static String Response;
static int i= (int)(Math.random()*((5-1)+1)+1);
static String r;
This is not helping your here, and if done right, shouldn't be needed.
The next issue is with i...
static int i = (int) (Math.random() * ((5 - 1) + 1) + 1);
public static void associate() {
if (i == 1) {
r = "Connect Angie";
} else if (i == 2) {
r = "*Cloud Laugh*";
} else if (i == 3) {
r = "Community";
} else if (i == 4) {
r = getMarco();
} else if (i == 5) {
r = "Indeed!";
}
Response = r;
}
i is never changed. Because it's static, you can create as many instance of Joketeller as you like and it will need change, so the response will always be the same.
While there are a number of possible ways to fix it, the simplest would be to remove all the static and make i a local variable within associate, as it's really not used anywhere else..
public void associate() {
int rnd = (int) (Math.random() * ((5 - 1) + 1) + 1);
if (rnd == 1) {
r = "Connect Angie";
} else if (rnd == 2) {
r = "*Cloud Laugh*";
} else if (rnd == 3) {
r = "Community";
} else if (rnd == 4) {
r = getMarco();
} else if (rnd == 5) {
r = "Indeed!";
}
response = r;
}
This means you don't need to create a new instance of Joketeller in order to get a different response.
For example....
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GUIWindow extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
GUIWindow wnd = new GUIWindow();
wnd.pack();
wnd.setLocationRelativeTo(null);
wnd.setVisible(true);
}
});
}
private Joketeller robot = new Joketeller();
private JLabel speakerlabel = new JLabel("Joke");
private JLabel marcoLabel = new JLabel("Marco");
private JTextField speakerfield = new JTextField("Enter Joke Here");
private JTextField marcofield = new JTextField("", 20);
private JButton jokebutton = new JButton("Recite Joke >>>");
public GUIWindow() {
JPanel dataPanel = new JPanel(new GridLayout(2, 2, 12, 16));
dataPanel.add(speakerlabel);
dataPanel.add(marcoLabel);
dataPanel.add(speakerfield);
dataPanel.add(marcofield);
JPanel buttonPanel = new JPanel();
buttonPanel.add(jokebutton);
Container container = getContentPane();
container.add(dataPanel, BorderLayout.CENTER);
container.add(buttonPanel, BorderLayout.SOUTH);
jokebutton.addActionListener(new JokeListener());
}
private class JokeListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String input = speakerfield.getText();
if (jokebutton.isEnabled()) {
robot.setJoke(input);
String Response = robot.getResponse();
marcofield.setText(Response);
}
}
}
public class Joketeller {
private String marco;
private String response;
private String r;
public void setMarco(String Joke) {
marco = Joke;
}
public void setJoke(String Joke) {
marco = Joke;
associate();
}
public String getJoke() {
return marco;
}
public String getMarco() {
return marco;
}
public void associate() {
int rnd = (int) (Math.random() * ((5 - 1) + 1) + 1);
if (rnd == 1) {
r = "Connect Angie";
} else if (rnd == 2) {
r = "*Cloud Laugh*";
} else if (rnd == 3) {
r = "Community";
} else if (rnd == 4) {
r = getMarco();
} else if (rnd == 5) {
r = "Indeed!";
}
response = r;
}
public String getResponse() {
return response;
}
}
}
I have a method that implements ActionListener. At the bottom of the class I have some get methods. However whenever I call the get methods in another class, I get a NullPointerException.
package com.FishingGame.Screen;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class HandleActions implements ActionListener{
private boolean goFishing = false, sell = false, checkW = false;
private int test = 12;
public HandleActions(){
}
void resetAllVars(){
goFishing = false;
sell = false;
checkW = false;
}
public void actionPerformed(ActionEvent e) {
String bSource = e.getActionCommand();
resetAllVars();
if(bSource == "go fishing"){
System.out.println("1");
goFishing = true;
}else if(bSource == "sell"){
System.out.println("2");
sell = true;
}else if(bSource == "check weather"){
System.out.println("3");
checkW = true;
}
}
public boolean getGoFishing(){
return goFishing;
}
public boolean getSell(){
return sell;
}
public boolean getCheckW(){
return checkW;
}
public int getTest(){
return test;
}
}
public class Game implements Runnable {
HandleActions h;
Window w;
public Game() {
w = new Window(600, 400, "Game");
w.add(w.mainScreen());
w.setVisible(true);
System.out.println(h.getTest());
}
Thread thread = new Thread(this);
#Override
public void run() {
}
public static void main(String args[]) {
Game g = new Game();
}
}
The console says the error is coming from the h.getTest() call. Is it something to do with the fact that the HandleActions class implements ActionListener. I can return values just fine from other classes.
the variable is uninitialized
HandleActions h;
public Game() {
h =new HandleActions(); //initialize
...
}
While developing a small task manager, I have noticed that columns aren't sorted correctly. To discard problems with my program, I have created a minimal version but it still fails to order the unique column right.
import java.awt.BorderLayout;
import java.util.List;
import java.util.Random;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
public class TableSortTest extends JFrame
{
private final JTable table;
private final ATableModel model;
public TableSortTest ()
{
setDefaultCloseOperation (EXIT_ON_CLOSE);
setSize (1366, 768);
setLocationRelativeTo (null);
model = new ATableModel ();
table = new JTable ();
table.setFillsViewportHeight (true);
table.setAutoCreateRowSorter (true);
table.setModel (model);
add (new JScrollPane (table), BorderLayout.CENTER);
setVisible (true);
Worker worker = new Worker ();
worker.execute ();
}
private class Pair
{
int index;
int value;
}
private class Worker extends SwingWorker <Void, Pair>
{
#Override
protected Void doInBackground ()
{
while (!isCancelled ())
{
Random r = new Random ();
for (int i = 0; i < 100; i++)
{
int indice = getIndexInRange (0, 99);
Pair p = new Pair ();
p.index = indice;
p.value = Math.abs (r.nextInt ());
publish (p);
}
try
{
Thread.sleep (1000);
}
catch (InterruptedException ie)
{
ie.printStackTrace ();
}
}
return null;
}
#Override
public void process (List <Pair> items)
{
for (Pair p : items)
{
model.setValueAt (p.value, p.index, 0);
}
}
}
public static int getIndexInRange (int min, int max)
{
return (min + (int) (Math.random () * ((max - min) + 1)));
}
private class ATableModel extends AbstractTableModel
{
private final Integer [] data;
public ATableModel ()
{
data = new Integer [100];
Random r = new Random ();
for (int i = 0; i < 100; i++)
{
data [i] = Math.abs (r.nextInt ());
}
}
#Override
public int getColumnCount ()
{
return 1;
}
#Override
public int getRowCount ()
{
return data.length;
}
#Override
public Object getValueAt (int rowIndex, int columnIndex)
{
return data [rowIndex];
}
#Override
public void setValueAt (Object value, int rowIndex, int columnIndex)
{
data [rowIndex] = (Integer) value;
fireTableRowUpdated (rowIndex, columnIndex);
}
#Override
public Class getColumnClass (int columnIndex)
{
return Integer.class;
}
#Override
public String getColumnName (int col)
{
return "Column";
}
}
public static final void main (String [] args)
{
SwingUtilities.invokeLater (() ->
{
try
{
new TableSortTest ();
}
catch (Exception e)
{
e.printStackTrace ();
}
});
}
}
I have tried with a ScheduledExecutorService + Runnable and a Timer + TimerTask just to test if it was a threading problem, but the behavior is the same. I have also read the Java Tutorial page about the subject. Given that my table only uses standard types I think that a simple table.setAutoCreateRowSorter (true); should do the job, shouldn't it?
Shouldn't the table be sorted after every modification/addition/removal even is fired?
Thanks for your quick answer trashgod. You're right, I meant fireTableRowsUpdated () but I made a mistake when I wrote the code, sorry. The point is that fireTableRowsUpdated (rowIndex, rowIndex) and fireTableCellUpdated (rowIndex, columnIndex) both fail to sort the column correctly. In the real program most of the table rows do change from one iteration to the next so calling fireTableDataChanged () makes perfect sense. But I didn't want to use it because if I select one or more rows to send a signal to the processes or whatever the selection is lost on every update. I have explored this way and found two forms of preserving the selection but it's a bit annoying and one of them breaks the selection with the keyboard. I show the necessary additions to the original code next.
The first form saves the selection before modifying the model and restores it after every update:
...
private class Worker extends SwingWorker <Void, Pair>
{
private int [] selectedRows;
#Override
protected Void doInBackground ()
{
while (!isCancelled ())
{
// Save the selection before modifying the model
int x = table.getSelectedRowCount ();
if (x > 0)
{
selectedRows = new int [x];
int [] tableSelection = table.getSelectedRows ();
for (int i = 0; i < x; i++)
{
selectedRows [i] = table.convertRowIndexToModel (tableSelection [i]);
}
}
Random r = new Random ();
for (int i = 0; i < table.getRowCount (); i++)
{
int indice = getIndexInRange (0, table.getRowCount () - 1);
Pair p = new Pair ();
p.index = indice;
p.value = Math.abs (r.nextInt ());
publish (p);
}
// If I put the code to restore the selection here, it doesn't work...
try
{
Thread.sleep (1000);
}
catch (InterruptedException ie)
{
ie.printStackTrace ();
}
}
return null;
}
#Override
public void process (List <Pair> items)
{
for (Pair p : items)
{
model.setValueAt (p.value, p.index, 1);
}
// Restore the selection on every update
if (selectedRows != null && selectedRows.length > 0)
{
for (int i = 0; i < selectedRows.length; i++)
{
table.addRowSelectionInterval (table.convertRowIndexToView (selectedRows [i]), table.convertRowIndexToView (selectedRows [i]));
}
}
}
}
...
The second form uses a ListSelectionListener, a KeyListener, and a flag. Selection with the keyboard doesn't work. To be honest, I don't know how did I come to get this solution. It probably was by chance:
public class TableSortTestSolucionConSelectionListener extends JFrame implements KeyListener
{
...
private boolean ctrlOrShiftDown = false;
private int [] selectedRows;
#Override
public void keyPressed (KeyEvent e)
{
ctrlOrShiftDown = e.isControlDown () || e.isShiftDown ();
}
#Override
public void keyReleased (KeyEvent e)
{
ctrlOrShiftDown = e.isControlDown () || e.isShiftDown ();
}
#Override
public void keyTyped (KeyEvent e)
{
ctrlOrShiftDown = e.isControlDown () || e.isShiftDown ();
}
public TableSortTestSolucionConSelectionListener ()
{
...
ListSelectionListener lsl = new ListSelectionListener ()
{
#Override
public void valueChanged (ListSelectionEvent e)
{
if (!e.getValueIsAdjusting ())
{
if (!ctrlOrShiftDown)
{
int x = table.getSelectedRowCount ();
if (x > 0)
{
selectedRows = new int [x];
int [] tableSelection = table.getSelectedRows ();
for (int i = 0; i < x; i++)
{
selectedRows [i] = table.convertRowIndexToModel (tableSelection [i]);
}
}
}
// Disable the listener to avoid infinite recursion
table.getSelectionModel ().removeListSelectionListener (this);
if (selectedRows != null && selectedRows.length > 0)
{
for (int i = 0; i < selectedRows.length; i++)
{
table.addRowSelectionInterval (table.convertRowIndexToView (selectedRows [i]), table.convertRowIndexToView (selectedRows [i]));
}
}
table.getSelectionModel ().addListSelectionListener (this);
}
}
};
table.getSelectionModel ().addListSelectionListener (lsl);
...
}
Fortunately today I have found a simple way to get the column sorted correctly and keep the current selection. You only have to add the following to your code:
TableRowSorter trs = (TableRowSorter) table.getRowSorter ();
trs.setSortsOnUpdates (true);
With this both fireTableCellUpdated () and fireTableRowsUpdated () work as I expected. To my understanding, setAutoCreateRowSorter () is only used to sort the rows when you click on the table header.
Greetings.
Using setSortsOnUpdates(), suggested here by #trcs, is the best general solution, but you may be able to optimize updates by the choice of TableModelEvent available to subclasses of AbstractTableModel.
The critical issue is the implementation of setValueAt(). If you meant fireTableRowsUpdated(), instead of fireTableRowUpdated(), note that the parameters represent a range of rows, not a row & column. In this case, because "all cell values in the table's rows may have changed," the revised example below invokes fireTableDataChanged(). I've also changed the model to manage a List<Integer> and normalized the size, N.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
/** #see https://stackoverflow.com/a/36522182/230513 */
public class TableSortTest extends JFrame {
private final JTable table;
private final ATableModel model;
public TableSortTest() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
model = new ATableModel();
table = new JTable(model){
#Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(200, 500);
}
};
table.setFillsViewportHeight(true);
table.setAutoCreateRowSorter(true);
add(new JScrollPane(table), BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
setVisible(true);
Worker worker = new Worker();
worker.execute();
}
private class Pair {
int index;
int value;
}
private class Worker extends SwingWorker<Void, Pair> {
private static final int N = 100;
private final Random r = new Random();
#Override
protected Void doInBackground() {
while (!isCancelled()) {
for (int i = 0; i < N; i++) {
int index = r.nextInt(N);
Pair p = new Pair();
p.index = index;
p.value = Math.abs(r.nextInt());
publish(p);
}
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
return null;
}
#Override
public void process(List<Pair> items) {
for (Pair p : items) {
model.setValueAt(p.value, p.index, 0);
}
}
}
private class ATableModel extends AbstractTableModel {
private static final int N = 100;
private final List<Integer> data = new ArrayList<>(N);
public ATableModel() {
final Random r = new Random();
for (int i = 0; i < N; i++) {
data.add(Math.abs(r.nextInt()));
}
}
#Override
public int getColumnCount() {
return 1;
}
#Override
public int getRowCount() {
return data.size();
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
return data.get(rowIndex);
}
#Override
public void setValueAt(Object value, int rowIndex, int columnIndex) {
data.set(rowIndex, (Integer) value);
fireTableDataChanged();
}
#Override
public Class getColumnClass(int columnIndex) {
return Integer.class;
}
#Override
public String getColumnName(int col) {
return "Column";
}
}
public static final void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new TableSortTest();
});
}
}
Recognizing that this is just an example, the variation below optimizes updates by publishing a List<Integer>, which is passed en bloc to the TableModel via process().
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
/**
* # see https://stackoverflow.com/a/36522182/230513
*/
public class TableSortTest extends JFrame {
private final JTable table;
private final ATableModel model;
public TableSortTest() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
model = new ATableModel();
table = new JTable(model) {
#Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(200, 500);
}
};
table.setFillsViewportHeight(true);
table.setAutoCreateRowSorter(true);
add(new JScrollPane(table), BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
setVisible(true);
Worker worker = new Worker();
worker.execute();
}
private class Worker extends SwingWorker<List<Integer>, List<Integer>> {
private static final int N = 100;
private final Random r = new Random();
private final List<Integer> data = new ArrayList<>(N);
#Override
protected List<Integer> doInBackground() throws Exception {
while (!isCancelled()) {
data.clear();
for (int i = 0; i < N; i++) {
data.add(Math.abs(r.nextInt()));
}
publish(data);
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
ie.printStackTrace(System.err);
}
}
return data;
}
#Override
protected void process(List<List<Integer>> chunks) {
for (List<Integer> chunk : chunks) {
model.update(chunk);
}
}
}
private class ATableModel extends AbstractTableModel {
private List<Integer> data = new ArrayList<>();
public void update(List<Integer> data) {
this.data = data;
fireTableDataChanged();
}
#Override
public int getColumnCount() {
return 1;
}
#Override
public int getRowCount() {
return data.size();
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
return data.get(rowIndex);
}
#Override
public Class getColumnClass(int columnIndex) {
return Integer.class;
}
#Override
public String getColumnName(int col) {
return "Column";
}
}
public static final void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new TableSortTest();
});
}
}
This is the class Gui.
import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JButton;
public class Gui extends JFrame{
Principal obiect;
public JButton heads = new JButton("Heads");
public JButton tails = new JButton("Tails");
public static JTextField display;
public JTextField comboul;
public JPanel panel = new JPanel();
public int predictie;
public Gui(){
super("HeadsOrTails");
panel.setLayout(new BorderLayout(100, 100));
panel.add(heads,BorderLayout.LINE_START);
panel.add(tails,BorderLayout.LINE_END);
panel.add(display,BorderLayout.CENTER); //HERE IS ERROR
panel.add(comboul,BorderLayout.PAGE_START);
}
public void dacaHeads(){
if(heads.getModel().isPressed()) predictie = 0;
}
public void dacaTails(){
if(tails.getModel().isPressed()) predictie = 1;
heads.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
dacaHeads();
obiect.flip();
if(predictie == obiect.returnStatus() ){
String s = comboul.getText();
int combo = Integer.valueOf(s);
s = Integer.toString(++combo);
comboul.setText(s);}
else{
String z = "0";
comboul.setText(z);
}
}
});
tails.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
dacaTails();
obiect.flip();
if(predictie == obiect.returnStatus() ){
String s = comboul.getText();
int combo = Integer.valueOf(s);
s = Integer.toString(++combo);
comboul.setText(s);}
else{
String z = "0";
comboul.setText(z);
}
}
});
}}
This is class Principal.
import javax.swing.*;
import java.util.Random;
public class Principal extends Gui {
public int combo;
public static Random bulion = new Random();
public static boolean sansa;
public static boolean input;
public int status;
//STATUS 0 = HEADS;
//STATUS 1 = TAILS;
public static void main(String[] args) {
Gui lee = new Gui(); //HERE IS ERROR
Principal obiect = new Principal();
lee.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lee.setSize(500,500);
lee.setVisible(true);
}
public int flip(){
boolean sansa2 ;
sansa2 = bulion.nextBoolean();
if(sansa2){
status = 0;
display.setText("Heads");
}
else{
status = 1;
display.setText("Tails");
}
return status;
}
public int returnStatus(){
return status;
}
}
These are the errors :
FIXED:
//Exception in thread "main" java.lang.StackOverflowError
//at Gui.(Gui.java:19) // Super
//at Principal.(Principal.java:3) //public class Principal extends Gui
//at Gui.(Gui.java:9) //Principal obiect = new Principal()
NOW:
Exception in thread "main" java.lang.NullPointerException
at Gui.<init>(Gui.java:23)
at Principal.main(Principal.java:14)
It's my first post so i am sorry if you don't understand.I'll post more information if you need.
When you create a Gui instance, it creates a Principal instance (since the Gui class has a Prinicipal instance variable - Principal obiect = new Principal();) which is also a Gui instance, so you get an infinite chain of constructor calls, leading to StackOverflowError.
To avoid it change
Principal obiect = new Principal();
to
Principal obiect;
and initialize this instance after the Gui instance is created (i.e. after the call to Gui lee = new Gui(); in your main method).
For example, add to Gui class :
public void setPrincipal (Principal object)
{
this.object = object;
}
And in your main :
Gui lee = new Gui();
lee.setPrincipal (new Principal());
For some reason (seemingly completely independent of code) the frame of my program will only appear some (maybe 20%) of the time. If I just continuously hit run I evently get the frame to show successfully, but it is never repeatable. has this ever happened to anyone else? I highly doubt this has to do with code but just in case
public class Trader{
public static Trader INSTANCE = new Trader();
JTextArea msgBox;
ApiController m_controller;
Connection connect;
Quotes quotes;
ArrayList<String> m_acctList = new ArrayList();
public static void main(String[] args) {
INSTANCE.run();
}
public void run(){
connect = new Connection();
}
public ArrayList<String> accountList() { return m_acctList; }
public ApiController controller() { return m_controller; }
public JFrame frame() { return connect.frame; }
}
next class
public class Connection implements IConnectionHandler{
static int port = 4001;
Quotes quotes;
JFrame frame;
CPanel panel;
boolean connected = false;
JTextArea logIn = new JTextArea();
JTextArea logOut = new JTextArea();
JTextArea msgBox = new JTextArea();
Logger loggerIn = new Logger(logIn);
Logger loggerOut = new Logger(logOut);
ApiController m_controller = new ApiController(this, loggerIn, loggerOut);
final ArrayList<String> m_acctList = new ArrayList<String>();
Connection(){
frame = new JFrame("Trader");
panel = new CPanel();
frame.add(panel);
frame.setSize(800,400);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
class CPanel extends JComponent{
Point frameLoc = frame.getLocation();
Dimension frameSize = frame.getSize();
JLabel text = new JLabel("Messages");
JScrollPane messages = new JScrollPane(msgBox);
CPanel(){
setLayout(null);
add(text);
text.setBounds(frameLoc.x+5, frameLoc.y+5,
text.getPreferredSize().width, text.getPreferredSize().height);
add(messages);
msgBox.setEditable(false);
msgBox.setLineWrap(true);
messages.setBounds(text.getLocation().x, text.getLocation().y+text.getSize().height+10,
720, 300);
m_controller.connect(null, port, 0);
}
}
#Override public void connected() {
show( "connected");
m_controller.reqCurrentTime( new ApiController.ITimeHandler() {
#Override public void currentTime(long time) {
show( "Server date/time is " + Formats.fmtDate(time * 1000) );
}
});
m_controller.reqBulletins( true, new ApiController.IBulletinHandler() {
#Override public void bulletin(int msgId, Types.NewsType newsType, String message, String exchange) {
String str = String.format( "Received bulletin: type=%s exchange=%s", newsType, exchange);
show( str);
show( message);
}
});
// Quotes quotes = new Quotes();
}
#Override public void disconnected() {
show( "disconnected");
}
#Override public void accountList(ArrayList<String> list) {
show( "Received account list");
m_acctList.clear();
m_acctList.addAll( list);
}
#Override public void error(Exception e) {
show( e.toString() );
}
#Override public void message(int id, int errorCode, String errorMsg) {
show( id + " " + errorCode + " " + errorMsg);
}
#Override public void show( final String str) {
SwingUtilities.invokeLater( new Runnable() {
#Override public void run() {
msgBox.append(str);
msgBox.append( "\n\n");
Dimension d = msgBox.getSize();
msgBox.scrollRectToVisible( new Rectangle( 0, d.height, 1, 1) );
}
});
}
private static class Logger implements ILogger{
final private JTextArea msgBox;
Logger(JTextArea area){
msgBox = area;
}
#Override public void log(final String str) {
SwingUtilities.invokeLater( new Runnable() {
#Override public void run() {
// m_area.append(str);
//
// Dimension d = m_area.getSize();
// m_area.scrollRectToVisible( new Rectangle( 0, d.height, 1, 1) );
}
});
}
}
}
and lastly
public class Quotes {
int numberOfStocks = 0;
JTextArea msgBox = Trader.INSTANCE.connect.msgBox;
String symbol;
File file = new File("/Users/spencerclayman/Desktop/IB_API/API_Data/stockList.txt");
ArrayList<quote> stockList = new ArrayList();
Quotes(){
msgBox.append("Getting stock quotes");
try(Scanner input = new Scanner(file);){
while(input.hasNextLine()){
symbol = input.nextLine();
newStock(newContract(symbol));
}
}
catch(Exception ex){msgBox.append("Error getting quotes");}
try{wait(10000);}catch(Exception ex){msgBox.append("error waiting - quote");}
for(int i = 0; i < numberOfStocks() -1 ; i++){
msgBox.append(stockList.get(i).m_description
+ " - " + stockList.get(i).m_last +"\n");
}
}
void newStock( NewContract contract) {
quote stock = new quote(contract.description());
stockList.add(stock);
Trader.INSTANCE.controller().reqTopMktData(contract, "", false, stock);
}
void newStock( quote stock) {
stockList.add(stock);
}
public void desubscribe() {
for (quote stock : stockList) {
Trader.INSTANCE.controller().cancelTopMktData(stock);
}
}
public int numberOfStocks(){
try(Scanner input = new Scanner(file);){
while(input.hasNextLine()){
numberOfStocks++;
input.nextLine();
}
}
catch(Exception ex){msgBox.append("Error getting symbols");}
return numberOfStocks;
}
static class quote extends TopMktDataAdapter{
String m_description;
double m_bid;
double m_ask;
double m_last;
long m_lastTime;
int m_bidSize;
int m_askSize;
double m_close;
int m_volume;
boolean m_frozen;
quote(String description){
m_description = description;
}
public String change() {
return m_close == 0 ? null : fmtPct( (m_last - m_close) / m_close);
}
#Override public void tickPrice( NewTickType tickType, double price, int canAutoExecute) {
switch( tickType) {
case BID:
m_bid = price;
break;
case ASK:
m_ask = price;
break;
case LAST:
m_last = price;
break;
case CLOSE:
m_close = price;
break;
}
}
#Override public void tickSize( NewTickType tickType, int size) {
switch( tickType) {
case BID_SIZE:
m_bidSize = size;
break;
case ASK_SIZE:
m_askSize = size;
break;
case VOLUME:
m_volume = size;
break;
}
}
#Override public void tickString(NewTickType tickType, String value) {
switch( tickType) {
case LAST_TIMESTAMP:
m_lastTime = Long.parseLong( value) * 1000;
break;
}
}
#Override public void marketDataType(Types.MktDataType marketDataType) {
m_frozen = marketDataType == Types.MktDataType.Frozen;
}
}
public NewContract newContract(String symbol){
NewContract c = new NewContract();
c.symbol(symbol);
c.secType(Types.SecType.STK);
c.exchange("SMART");
c.currency("USD");
return c;
}
}
again i am highly doubtful that code has anything to do with it. Im assumign its a problem with the IDE.