My problem is that my JavaFX TableView cannot display my String that is crypted with SHA-256. It works with a normal string. Here my code for the my scramble method.
private String scrambleName(String name, String surname)
{
MessageDigest messageDigest;
String cryptedName;
StringBuilder builder = new StringBuilder();
builder.append(name);
builder.append(" ");
builder.append(surname);
String fullName = builder.toString();
try
{
messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest.update(fullName.getBytes());
cryptedName = new String(messageDigest.digest());
return cryptedName;
} catch (NoSuchAlgorithmException ex)
{
Logger.getLogger(Scrambler.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
This method returns a string for my scrambled vote object.
public class ScrambledVote
{
private int key;
private String scrambledName;
private int age;
private String party;
public ScrambledVote(int key, String scrambledName, int age, String party)
{
this.key = key;
this.scrambledName = scrambledName;
this.age = age;
this.party = party;
}
//Getter Setter
I load the data from a file. The command line output works fine. But the TableView throws an exception.
Exception in thread "JavaFX Application Thread" java.lang.ArrayIndexOutOfBoundsException: 0
at com.sun.javafx.font.directwrite.DWGlyphLayout.getIndices(DWGlyphLayout.java:198)
at com.sun.javafx.font.directwrite.DWGlyphLayout.renderShape(DWGlyphLayout.java:401)
at com.sun.javafx.font.directwrite.DWGlyphLayout.layout(DWGlyphLayout.java:154)
at com.sun.javafx.text.PrismTextLayout.shape(PrismTextLayout.java:832)
at com.sun.javafx.text.PrismTextLayout.layout(PrismTextLayout.java:1062)
at com.sun.javafx.text.PrismTextLayout.ensureLayout(PrismTextLayout.java:221)
at com.sun.javafx.text.PrismTextLayout.getBounds(PrismTextLayout.java:244)
at com.sun.javafx.scene.control.skin.Utils.computeTextHeight(Utils.java:129)
at com.sun.javafx.scene.control.skin.LabeledSkinBase.computePrefHeight(LabeledSkinBase.java:832)
at javafx.scene.control.Control.computePrefHeight(Control.java:543)
at javafx.scene.Parent.prefHeight(Parent.java:918)
at javafx.scene.layout.Region.prefHeight(Region.java:1438)
at com.sun.javafx.scene.control.skin.TableRowSkinBase.computePrefHeight(TableRowSkinBase.java:544)
at javafx.scene.control.Control.computePrefHeight(Control.java:543)
at javafx.scene.Parent.prefHeight(Parent.java:924)
at javafx.scene.layout.Region.prefHeight(Region.java:1438)
at com.sun.javafx.scene.control.skin.VirtualFlow.resizeCellSize(VirtualFlow.java:1782)
at com.sun.javafx.scene.control.skin.VirtualFlow.addTrailingCells(VirtualFlow.java:1212)
at com.sun.javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1102)
at javafx.scene.Parent.layout(Parent.java:1076)
at javafx.scene.Parent.layout(Parent.java:1082)
at javafx.scene.Parent.layout(Parent.java:1082)
at javafx.scene.Scene.doLayoutPass(Scene.java:576)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2386)
at com.sun.javafx.tk.Toolkit$3.run(Toolkit.java:321)
at com.sun.javafx.tk.Toolkit$3.run(Toolkit.java:319)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:319)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:348)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:479)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:460)
at com.sun.javafx.tk.quantum.QuantumToolkit$13.run(QuantumToolkit.java:327)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
at java.lang.Thread.run(Thread.java:745)
How can my TableView display this kind of scrambled String?
@FXML
protected void loadFile()
{
allScrambledVotes.clear();
HashTable hashTable = new HashTable(txtPath.getText());
LinkList[] allVotes = hashTable.getArray();
Scrambler scrambler = new Scrambler();
int k = 0;
for (LinkList vote : allVotes)
{
for (int j = 0; j < vote.getSize(); j++)
{
Vote v = vote.findVote(j);
scrambler.put(k, v);
allScrambledVotes.add(scrambler.get(k));
System.out.println(allScrambledVotes.get(k));
k++;
}
}
table.setItems(allScrambledVotes);
System.out.println("Hello World");
}
I forget my loadFile method. I load the data from a hashtable with my own functions. The error occurs after the Hello World sysout.
The
digest()
method returns an array of bytes: these are not the ascii values of the string representation of the digest. You're getting the error (I think) because JavaFX is unable to render a character for some of the byte values you have.Instead of
try something along the lines of
Here is a complete example: