boa tarde
Alguem sabe como faço para interpretar um campo vazio de um arquivo texto como null para lê-lo e gravá-lo posteriormente.
Estou tentando fazer mas gera a seguinte excessão:
Exception in thread "AWT-EventQueue-0" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)
at santandertotaluf.Leitura.Leia(Leitura.java:53)
at santandertotaluf.Janela.jButton2ActionPerformed(Janela.java:111)
at santandertotaluf.Janela.access$200(Janela.java:18)
at santandertotaluf.Janela$3.actionPerformed(Janela.java:59)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6041)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5806)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4413)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2440)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
o que ocorre é o seguinte:
estou lendo um arquivo dessa forma:
public static void Leia(String arquivo) throws FileNotFoundException, IOException, ClassNotFoundException, SQLException {
BufferedReader naLinha = null;
Class.forName("org.gjt.mm.mysql.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://192.168.1.56/santander", "root", "root");
naLinha = new BufferedReader(new FileReader(arquivo));
String linha = null;
try {
sqlDelete = "delete from kg_uf";
Statement smtpDelete = con.prepareStatement(sqlDelete);
smtpDelete.execute(sqlDelete);
while ((linha = naLinha.readLine()) != null) {
StringTokenizer st = new StringTokenizer(linha,";");
cpfCnpj = st.nextToken();
contrato = st.nextToken();
valorParcela = st.nextToken();
nomeDevedor = st.nextToken();
uf = st.nextToken();
System.out.println(cpfCnpj + contrato + valorParcela + nomeDevedor + uf);
sqlInsert = "insert into kg_uf (cpfCnpj,contrato,valorParcela,nomeDevedor,uf) values('" + cpfCnpj + "','" + contrato + "','" + valorParcela + "','" + nomeDevedor + "','" + uf + "')";
Statement smtpInsert = con.prepareStatement(sqlInsert);
smtpInsert.execute(sqlInsert);
}
naLinha.close();
con.close();
} catch (IOException err) {
JOptionPane.showMessageDialog(null, err.getMessage());
}
}
}
quando chega na leitura do valor uf no arquivo texto ele está em branco como no exemplo abaixo
16648979800;0006010214372000261;00000000000000066580;RONALDO;SP;
16648979800;0006000208580001287;00000000000000069793;RONALDO;SP;
17347689800;0021010263760000152;00000000000000148019;TEODORO;;
nesse caso as duas primeiras linhas ele lê normalmente, agora na terceira ele não consegue acessar o elemento porque não existe elemento para acessar.
existe alguma maneira de quando na leitura ele encontrar esse tipo de caso interpretar isso como nulo e passar esse valor nulo para a variavel uf ao invés de gerar excessão e abortar a leitura?