Reputation: 1
I'm currently trying to create a symbol table using the Map and treeMap packages, but when I try to add an item into the table, I get this error: "Cannot make a static reference to the non-static field STTM".
SymbolTableTM class:
import java.util.Map;
import java.util.TreeMap;
public class SymbolTableTM<Key extends Comparable<Key>, Value> {
Map<Key, Value> treeMap = new TreeMap<>();
public SymbolTableTM() {
}
public int[] min() {
int min = Integer.MAX_VALUE;
int[] min_arr = new int[2];
for(Map.Entry<Key, Value> entry: treeMap.entrySet()) {
if((Integer) entry.getValue() < min) {
min = (Integer) entry.getValue();
min_arr[0] = (Integer) entry.getKey();
min_arr[1] = min;
}
}
return min_arr;
}
public int[] max() {
int max = Integer.MIN_VALUE;
int[] max_arr = new int[2];
for(Map.Entry<Key, Value> entry: treeMap.entrySet()) {
if((Integer) entry.getValue() > max) {
max = (Integer) entry.getValue();
max_arr[0] = (Integer) entry.getKey();
max_arr[1] = max;
}
}
return max_arr;
}
public void delete(Key key) {
treeMap.remove(key);
}
public Value get(Key key) {
return treeMap.get(key);
}
public void put(Key key, Value val) {
treeMap.put(key, val);
}
}
DriverTM class:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class DriverTM<Key extends Comparable<Key>, Value> {
SymbolTableTM<Key, Value> STTM = new SymbolTableTM<>();
public static void main(String[] args) {
try {
File myFile = new File("lab3in.txt");
Scanner scanner = new Scanner(myFile);
while(scanner.hasNext()) {
STTM.put((Integer) scanner.nextInt(), (Integer) scanner.nextInt());
}
scanner.close();
}
catch(FileNotFoundException e){
System.out.println("An error occurred");
e.printStackTrace();
}
}
}
My issue is on line 16 of the DriverTM class.
I thought using an object (STTM) to refer to a non static field wouldn't be an issue, but obviously I'm missing something. Any help appreciated.
Upvotes: 0
Views: 30