Erro ao Excluir

:cry:

Aparece o seguinte erro:

[quote]Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException
at Apresentação.Excluir.actionPerformed(Excluir.java:210)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
[/quote]

A linha 210 é essa: rs = MeuState.executeQuery(SQL);

e meu código completo é esse:

[code] package Apresentação;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class Excluir extends JFrame implements ActionListener{
JLabel tn, td, tv, tq, back;
ImageIcon I1, I2, s1, s2, bg;
JTextField tnome, tdes, tvalor, tquan;
JButton Excluir, Sair, pro, ant, ult, pri;
ResultSet rs;
Statement MeuState;
static JFrame ex = new Excluir();
Excluir() {
setTitle(“Inserindo Dados”);
setSize(400,250);
setLocationRelativeTo(null);
setResizable(false);
getContentPane().setLayout(null);

	/**COMEÇO DO BLOCO LABEL NOME! E  TEXTFIELD NOME */
	tn = new JLabel("Nome...");
	tn.setSize(100,20);
	tn.setLocation(30,25);
	getContentPane().add(tn);

	tnome = new JTextField();
	tnome.setSize(120,20);
	tnome.setLocation(120,26);
	getContentPane().add(tnome);

	/**COMEÇO DO BLOCO LABEL NOME! E  TEXTFIELD NOME */


	/*COMEÇO DO BLOCO LABEL Descrição! E  TEXTFIELD Descrição */
	td = new JLabel("Descrição...");
	td.setSize(100,20);
	td.setLocation(30,65);
	getContentPane().add(td);

	tdes = new JTextField();
	tdes.setSize(120,20);
	tdes.setLocation(120,65);
	getContentPane().add(tdes);


	/*FIM DO BLOCO LABEL Descrição! E  TEXTFIELD Descrição */

	/**COMEÇO DO BLOCO LABEL Valor! E  TEXTFIELD Valor*/
	tv = new JLabel("Valor...");
	tv.setSize(100,20);
	tv.setLocation(30,105);
	getContentPane().add(tv);

	tvalor = new JTextField();
	tvalor.setSize(120,20);
	tvalor.setLocation(120,105);
	getContentPane().add(tvalor);

	/**FIM DO BLOCO LABEL Valor! E  TEXTFIELD Valor */


	/**COMEÇO DO BLOCO LABEL Quantidade! E  TEXTFIELD Quantidade */
	tq = new JLabel("Quantidade...");
	tq.setSize(100,20);
	tq.setLocation(30,145);
	getContentPane().add(tq);

	tquan = new JTextField();
	tquan.setSize(120,20);
	tquan.setLocation(120,145);
	getContentPane().add(tquan);
	
	
	Excluir = new JButton("Excluir");
	Excluir.setSize(80,50);
	Excluir.setLocation(280,25);
	Excluir.addActionListener(this);
	getContentPane().add(Excluir);

	Sair = new JButton("Sair");
	Sair.setSize(80,50);
	Sair.setLocation(280,85);

	Sair.addActionListener(this);
	getContentPane().add(Sair);
	
	pri = new JButton("|<");
	pri.setSize(50,20);
	pri.setLocation(80,180);

	pri.addActionListener(this);
	getContentPane().add(pri);
	
	ant = new JButton("<");
	ant.setSize(50,20);
	ant.setLocation(140,180);
	ant.addActionListener(this);
	getContentPane().add(ant);
	
	pro = new JButton(">");
	pro.setSize(50,20);
	pro.setLocation(190,180);
	pro.addActionListener(this);
	getContentPane().add(pro);
	
	ult = new JButton(">|");
	ult.setSize(50,20);
	ult.setLocation(250,180);
	ult.addActionListener(this);
	getContentPane().add(ult);
	
	
String url ="jdbc:odbc:bdconsulta";
	try
	{
		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		Connection MinhaConexao = DriverManager.getConnection(url);
		Statement MeuState = MinhaConexao.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
	rs = MeuState.executeQuery("SELECT * From Produtos");
		rs.first();
		atualizaCampos();
		
		
				}
	catch(ClassNotFoundException ex)
	{
	System.out.println("Driver não encontrado");
	
	}
	catch(SQLException ex)
	{
	System.out.println("Problemas na conexão com a fonte de dados");
	
	}

}

public static void main(String[] args) {
ex.show();
ex.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}

public void actionPerformed(ActionEvent e) 
{
	try
	{
		if(e.getSource()==pri)
		{
			rs.first();
		atualizaCampos();
		}
		if(e.getSource()==ant)
		{
			rs.previous();
		atualizaCampos();
		}
		if(e.getSource()==pro)
		{
			rs.next();
		atualizaCampos();
		}
		if(e.getSource()==ult)
		{
			rs.last();
			atualizaCampos();
		}
		if(e.getSource()==Excluir)
		[color=darkred]{

			try 
			{
				String SQL = "SELECT Nome From Filmes Where Nome = '"+tnome.getText()+"'";
				rs = MeuState.executeQuery(SQL);
				String nome = "";
				try
				{
					rs.next();
					nome = "Deletar o Filme: "+rs.getString("Nome");
					
				}
				catch(SQLException ex)
				{
					JOptionPane.showMessageDialog(null,"Filme não cadastrado!");
				}
				int n = JOptionPane.showConfirmDialog(null,nome," ",JOptionPane.YES_NO_OPTION);
				if(n==JOptionPane.YES_OPTION)
				{
					SQL = "DELETE FROM FRILMES Where Nome = '"+tnome.getText()+"'";
					int r = MeuState.executeUpdate(SQL);
					if(r==1)
						JOptionPane.showMessageDialog(null,"Exclusão com sucesso");
					else
						JOptionPane.showMessageDialog(null,"Não foi possivel");
					
				}
				
				
			}
			catch(SQLException ex) 
			{
				
			}
			
			
		}
	
	} [/color]
	catch(SQLException ex)
	{
		System.out.println("Erro navegador");
	}
	
}
public void atualizaCampos()
{

	try
	{
		tnome.setText(rs.getString("Nome"));
		tdes.setText(rs.getString("Descrição"));
		tvalor.setText(rs.getString("Valor"));
		tquan.setText(rs.getString("Valor"));
		
	}
	catch(SQLException ex)
	{
		
	}
	
}

}
[/code]

Se alguém poder me dar uma luz, fico grato.
Brigado desde de já!

Vi que sua query ta errada "FRILMES" -> "FILMES":

DELETE FROM [color=red]FRILMES [/color] Where Nome =

Com erro na query a var r nem é inicializada para ser tratado no if abaixo gerando “NullPointerException”

[quote]String url =“jdbc:odbc:bdconsulta”;
try
{
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection MinhaConexao = DriverManager.getConnection(url);[/quote]

Tente o seguinte, colocar usuário e senha pra fazer conexão com o banco:

String url ="jdbc:odbc:bdconsulta"; String user = ""; String password = ""; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection MinhaConexao = DriverManager.getConnection(url, user, password);
T+

Fiz as 2 coisas que citaram e continua dando erro :confused:

Se vcs repararem, eu inicio o bd na classe da tela;
Não tem nenhuma relação com o problema né?

O que você fez foi “esconder” uma variável de instância (MeuState) com a definição de uma variável local (MeuState).

Isso fez com que quando você quis usar “MeuState” em outros lugares, houvesse um NullPointerException.

Remova a definição da variável local.

Statement MeuState; // deixe como está
...
// removendo a definição da variável local:
MeuState = MinhaConexao.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);  
rs = MeuState.executeQuery("SELECT * From Produtos");  

A propósito, Java não é C++ ou C#, e em Java costuma-se seguir os padrões de codificação da Sun.

Se seguirmos esses padrões, fica mais fácil dizer quando um nome é um nome de variável ou de método (exemplo: meuState, excluirCandidato()), de constante (exemplo TAMANHO_MAXIMO), ou de classe (exemplo: MinhaConexao).

é, exatamente esse foi o problema, brigado cara…
meu professor sempre fala isso de codificação ;D

Mas agora surgiu outro problema, com meus navegadores…
qnd eu clico no nevagador dps de excluir surge este erro:

do try catch(SQLException ex) :

		System.out.println("Erro navegador");

o que pode ser?

[quote]Consigui !
mas so funciona meu botão “rs.last()” e o “rs.first()”
pq?

eu adcionei em cada botao a seguinte linha:

rs = MeuState.executeQuery("SELECT * From Produtos");[/quote]

consigui resolver este problema da seguinte maneira:

qnd eu clico no botão excluir volta ao antigo rs
desta maneira: [code]if(r==1){

						JOptionPane.showMessageDialog(null,"Exclusão com sucesso");
						rs = MeuState.executeQuery("SELECT * From Produtos");
						rs.first();
						atualizaCampos();
					}else[/code]

Porém, dps de clicar algumas vezes no next e no previous o botão começa responder dps de 2 ou 3 clicks
oque eu posso fazer para evitar este"erro?"