0
I am having this error when in my program we have two classes with referenced methods.
System.Nullreferenceexception was unhandled HResult=-2147467261 Message=Object Reference not set to an instance of an Object. Source=Programming in Commercial Languages Stacktrace: At programacao_em_linguagens_comerciais.FormCadastroUsuario.button1_Click(Object Sender, Eventargs e) in d: users Lucas Documents visual studio 2015 Projects Programming in Commercial Languages Programming in Commercial Languages Tela_cadastro_usuario.Cs:line 63 At System.Windows.Forms.Control.Onclick(Eventargs and) At System.Windows.Forms.Button.Onclick(Eventargs and) At System.Windows.Forms.Button.Onmouseup(Mouseeventargs mevent) At System.Windows.Forms.Control.Wmmouseup(Message& m, Mousebuttons button, Int32 clicks) At System.Windows.Forms.Control.Wndproc(Message& m) At system.Windows.Forms.ButtonBase.Wndproc(Message& m) At system.Windows.Forms.Button.Wndproc(Message& m) At System.Windows.Forms.Control.Controlnativewindow.Onmessage(Message& m) At System.Windows.Forms.Control.Controlnativewindow.Wndproc(Message& m) At system.Windows.Forms.NativeWindow.Debuggablecallback(Intptr hwnd, Int32 msg, Intptr wparam, Intptr lparam) At system.Windows.Forms.UnsafeNativeMethods.Dispatchmessagew(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) At system.Windows.Forms.Application.Threadcontext.Runmessageloopinner(Int32 Reason, Applicationcontext context) At system.Windows.Forms.Application.Threadcontext.Runmessageloop(Int32 Reason, Applicationcontext context) At System.Windows.Forms.Application.Run(Form mainForm) At programacao_em_linguagens_comerciais.Program.Main() in d: users Lucas Documents visual studio 2015 Projects Programacao em Linguagens Comerciais Programacao em Linguagens Comerciais Program.Cs:line 19 At system.AppDomain. _nExecuteAssembly(Runtimeassembly Assembly, String[] args) At System.AppDomain.Executeassembly(String assemblyFile, Evidence assemblySecurity, String[] args) At microsoft.VisualStudio.Hostingprocess.HostProc.Runusersassembly() At System.Threading.Threadhelper.Threadstart_context(Object state) At system.Threading.Executioncontext.Runinternal(Executioncontext executionContext, Contextcallback callback, Object state, Boolean preserveSyncCtx) At System.Threading.Executioncontext.Run(Executioncontext executionContext, Contextcallback callback, Object state, Boolean preserveSyncCtx) At System.Threading.Executioncontext.Run(Executioncontext executionContext, Contextcallback callback, Object state) At System.Threading.Threadhelper.Threadstart() Innerexception:
The error occurs when I am trying to write the field to the database Usuario.login.login, he goes through the recording part Username. and when it comes to what it would be Usuario.login.login he gives this error, would know tell me what I’m doing wrong ? The code is auto-incremented in the database.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class Login
{
public Login() { }
public String login;
public String senha;
public bool tipo;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Usuario
{
public Usuario() { }
public int codigo;
public String nome;
public Login login;
}
The part that has the bug, in it I do the check with the IF to see if any of the fields are not empty. I don’t know why it keeps returning this error message if it will never happen the if
if any of the fields are empty.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Programacao_em_Linguagens_Comerciais
{
public partial class FormCadastroUsuario : Form
{
public FormCadastroUsuario()
{
InitializeComponent();
}
public void FormCadastroUsuario_Load(object sender, EventArgs e)
{
}
public void label1_Click(object sender, EventArgs e)
{
}
public void textBox3_TextChanged(object sender, EventArgs e)
{
}
public void textBox2_TextChanged(object sender, EventArgs e)
{
}
public void textBox1_TextChanged(object sender, EventArgs e)
{
}
public void textBox4_TextChanged(object sender, EventArgs e)
{
}
public void textBox5_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Usuario Entrada = new Usuario();
new CadastroUsuarioService().Gravar(Entrada);
if (TXTCadastroUsuarioNome.Text != "" && TXTCadastroUsuarioLogin.Text != "" && TXTCadastroUsuarioSenha.Text != "")
{
Entrada.nome = TXTCadastroUsuarioNome.Text;
Entrada.login.login = TXTCadastroUsuarioLogin.Text;
Entrada.login.senha = TXTCadastroUsuarioSenha.Text;
MessageBox.Show("usuario cadastrado.");
}
// MessageBox.Show("Autenticacao efetuada com sucesso");
// this.Hide();
//FormCadastroUsuario Tela = new FormCadastroUsuario();
//Tela.Visible = true;
// MessageBox.Show("Falha na autenticacao");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Programacao_em_Linguagens_Comerciais
{
class CadastroUsuarioService
{
public void Gravar(Usuario usuario)
{
new CadastroUsuarioDAO().Gravar(usuario);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Web;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace Programacao_em_Linguagens_Comerciais
{
class CadastroUsuarioDAO
{
MySqlConnection conn = null;
private String connectionString = "Server=localhost;Database=DB_Atps_Prog_Ling_Comerc;Uid=Lucas;Pwd=123;";
public CadastroUsuarioDAO()
{
OpenCon();
}
public void OpenCon()
{
if (conn == null)
{
conn = new MySqlConnection(connectionString);
}
if (conn.State == System.Data.ConnectionState.Closed)
{
conn.Open();
}
}
public void CloseCon()
{
if (conn != null && conn.State == System.Data.ConnectionState.Open)
{
conn.Close();
}
}
public void Gravar(Usuario usuario)
{
try
{
if (usuario.nome != "" && usuario.login.login != "" && usuario.login.senha != "")
{
String comandoMysql = "insert into Usuario(nome,login,senha)"
+ "values ('" + usuario.nome + "','" + usuario.login.login + "','" + usuario.login.senha + "')";
MySqlCommand cmd = new MySqlCommand(comandoMysql, conn);
// cmd.ExecuteNonQuery();
}
else
{
}
}
catch
{
}
}
}
}
You instantiate a user, but not the user login, so it is null.
– ptkato
I tried to search on the internet and I couldn’t find something like this that you said, I had already looked but I don’t know how to explain what it is nor use the term to do this, could you show me how ? doesn’t have to be on top of my code can make an example with Classea.ClasseB.Attribute 1
– Lucas