Added minor error handling, fixed NullPointer

workbranch
asdfasdf 6 years ago
parent 7ab1b38684
commit 85c1de8f3a

@ -5,6 +5,7 @@ import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Objects;
import java.io.IOException;
public abstract class DataSource {
protected HashMap<String, StockType> targets;
@ -65,5 +66,5 @@ public abstract class DataSource {
return Objects.hash(name);
}
public abstract double[] update(String stype, String idstring);
public abstract double[] update(String stype, String idstring) throws IOException;
}

@ -1,6 +1,7 @@
package data;
import java.util.Objects;
import java.io.IOException;
public class StockEntry {
private String identifier;
@ -17,7 +18,14 @@ public class StockEntry {
public boolean update() {
boolean changed = false;
updating = true;
double[] result = this.stype.update(this.identifier);
double[] result;
try {
result = this.stype.update(this.identifier);
} catch (IOException e) {
System.out.println("Error getting update");
e.printStackTrace();
return false;
}
if ((result[0] != price) || (result[1] != change)) {
changed = true;
}

@ -1,6 +1,7 @@
package data;
import java.util.*;
import java.io.IOException;
public abstract class StockType {
protected String name;
@ -14,7 +15,7 @@ public abstract class StockType {
//Effects: return current price[0] and %change[1]
// (2 element array)
//Require: working sources
public double[] update(String idstring) {
public double[] update(String idstring) throws IOException {
double[] result = {0.0, 0.0};
for (DataSource source : sources) {
double[] sourceRes = source.update(name, idstring);

@ -142,11 +142,9 @@ public class WatchList extends Observable implements Load,Save {
}
sentry.update();
}
if (changed) {
setChanged();
notifyObservers(new Wevent(Etype.UPDATE));
}
}
public class Wevent {
private Etype type;

@ -13,7 +13,7 @@ public class AlphaVantage extends DataSource {
}
@Override
public double[] update(String stype, String idstring) {
public double[] update(String stype, String idstring) throws IOException {
double[] result = {0.0, 0.0};
try {
String urlString = Net.urlStringBuilder(url, "function", "TIME_SERIES_INTRADAY", "symbol", idstring,
@ -21,17 +21,20 @@ public class AlphaVantage extends DataSource {
"apikey", apiKey);
JsonObject response = StockJson.urlToJson(urlString);
JsonObject preJson = StockJson.jsonInJson(response, "Time Series (5min)");
if (preJson == null) {
throw new IOException("Error getting data from " + name);
}
JsonObject mainJson = StockJson.timeSeriesElement(preJson, 0);
System.out.print(mainJson);
//System.out.print(mainJson);
result[0] = Double.parseDouble(StockJson.stringGetter(mainJson, "4. close"));
Double open = Double.parseDouble(StockJson.stringGetter(mainJson, "1. open"));
result[1] = (result[0] - open) / open;
} catch (ParaMismatchException e) {
e.printStackTrace();
} catch (IOException e) {
System.out.println("Error getting data from: " + name);
} //catch (IOException e) {
// System.out.println("Error getting data from: " + name);
//e.printStackTrace();
}
//}
return result;
}
}

@ -47,6 +47,7 @@ public class StockJson {
// From https://stackoverflow.com/questions/33531041/jsonobject-get-value-of-first-node-regardless-of-name
public static JsonObject timeSeriesElement(JsonObject jobj, int index) {
String name = (String) jobj.keySet().toArray()[index];
System.out.println(name);
return jsonInJson(jobj, name);
}
}

@ -11,7 +11,10 @@ public class AddButt extends JButton {
public AddButt(WatchList wlist) {
super("Add Stock...");
//https://coderanch.com/t/580497/java/JButton-clicked-action
if (getActionListeners().length < 1) {
addActionListener(new AddButtList());
}
this.wlist = wlist;
}
@ -19,9 +22,9 @@ public class AddButt extends JButton {
public void actionPerformed(ActionEvent e) {
//https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html#input
//https://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html
System.out.println("Count of listeners: " + ((JButton) e.getSource()).getActionListeners().length);
String userin = (String)JOptionPane.showInputDialog("Enter the identifier:");
if ((userin != null) && (userin.length() > 0)) {
//https://stackoverflow.com/questions/12771500/best-way-of-creating-and-using-an-anonymous-runnable-class
new Thread(new Runnable() {
@Override
public void run() {

@ -12,7 +12,9 @@ public class DelButt extends JButton {
public DelButt(WatchList wlist) {
super("Remove Stock...");
if (getActionListeners().length < 1) {
addActionListener(new DelButtList());
}
this.wlist = wlist;
}

@ -10,7 +10,9 @@ public class UpButt extends JButton {
public UpButt(WatchList wlist) {
super("Update");
if (getActionListeners().length < 1) {
addActionListener(new UpButtList());
}
this.wlist = wlist;
}

@ -8,6 +8,7 @@ import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
public class NasdaqTest {
private StockType naasdaq;
@ -24,6 +25,10 @@ public class NasdaqTest {
@Test
public void testUpdate() {
try {
double[] farray = naasdaq.update("MSFT");
} catch (Exception e) {
fail();
}
}
}

@ -8,6 +8,7 @@ import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
public class NyseTest {
private StockType nyyyse;
@ -24,7 +25,11 @@ public class NyseTest {
@Test
public void testUpdate() {
try {
double[] farray = nyyyse.update("MSFT");
} catch (Exception e) {
fail();
}
}
@Test

Loading…
Cancel
Save