Errors on HistogramAlphabet with Map function [closed]
I have fixed all of the errors from the previous code but when I try to run my program I get the following errors from my terminal
"java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:116) at java.base/java.lang.reflect.Method.invoke(Method.java:577) at javafx.graphics@18.0.1/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465) at javafx.graphics@18.0.1/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) at java.base/java.lang.reflect.Method.invoke(Method.java:577) at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1081) Caused by: java.lang.NullPointerException: Cannot invoke "java.util.Map.put(Object, Object)" because "this.slicePieChart" is null at com.example.assignment3/com.example.assignment3.HistogramAlphabet$myPieChart.getMyPieChart(HistogramAlphabet.java:131) at com.example.assignment3/com.example.assignment3.HistogramAlphabet$myPieChart.(HistogramAlphabet.java:114) at com.example.assignment3/com.example.assignment3.Assignment3.readFile(Assignment3.java:61) at com.example.assignment3/com.example.assignment3.Assignment3.main(Assignment3.java:93) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ... 6 more Exception running application com.example.assignment3.Assignment3"
for reference: here's the codes that are being problematic
in HistogramAlphabet.java, my inner class myPieChart
public class myPieChart{
Map<Character, Slice> slicePieChart;
public Map<Character, Slice> getMyPieChart;
int N;
myPoint center;
int radius;
double rotateAngle;
myColor[] color;
myPieChart(int N, myPoint center, int radius, double rotateAngle, myColor[] color){
this.N = N;
this.center = center;
this.radius = radius;
this.color = color;
this.rotateAngle = Optional.ofNullable(rotateAngle).orElse(0.0);
probability = getProbability();
slicePieChart = getMyPieChart();
}
public Map<Character, Slice> getMyPieChart(){
myColor[] colors = myColor.getMyColors();
Random rand = new Random();
int colorsSize = colors.length;
double startAngle = rotateAngle;
for(Character Key: probability.keySet()) {
double angle = 360.0 * probability.get(Key);
slicePieChart.put(Key, new Slice(center, radius, startAngle, angle, new myColor[]{colors[rand.nextInt(colorsSize)]}));
}
return slicePieChart;
}
and here is Assignment3.java that's been causing issues
public static void readFile() {
try {
String w = "";
while (input.hasNext()) {
w += input.nextLine().replaceAll("[^a-zA-Z]", "").toLowerCase();
}
System.out.println("\nNumber of Characters: " + w.length() + "\n");
HistogramAlphabet H = new HistogramAlphabet(w);
Map<Character, Integer> sortedFrequency = H.sortDownFrequency();
sortedFrequency.forEach((K, V) -> System.out.println(K + ": " + V));
System.out.println("\nCumulative Frequency: " + H.getCumulativeFrequency() + "\n");
System.out.println("\nNumber of Characters: " + sortedFrequency.values().stream().reduce(0, (x,y)-> x +y)+ "\n");
HistogramAlphabet.myPieChart piechart = H.new myPieChart(0, new myPoint(100,100), 50, 30.0, color);
System.out.println(H.getProbability());
System.out.println("\nSum of Probabilities: " + H.getSumOfProbability() + "\n");
Map<Character, Slice> slices = piechart.getMyPieChart;
sortedFrequency.forEach((K,V) -> System.out.println(K + ": " + slices.get(K)));
double sumofangles = 0.0;
for(Character key : slices.keySet()){ sumofangles += slices.get(key).getAngle();}
System.out.println("\nSum of Angles: " + sumofangles);
} catch (NoSuchElementException elementException) {
System.err.println("Invalid Input! Terminating....");
} catch (IllegalStateException stateException) {
System.out.println("Error processing file! Terminating...");
}
}
Comments
Post a Comment