0
I am having this error when trying to implement a theme search system in the database. The chronological order of events is as follows:
A screen is shown for the user to enter the desired questions, and the user is also shown which theme is currently selected, which question will be "on". There is a button, just below the theme name, which allows the user to select one of the existing themes in the database.
When the select theme button is clicked, a Panel type object is placed in front of the camera with an Inputfield responsible for receiving the desired theme name, either a piece of the name or the entire name, and by clicking "Search", the components which, at the beginning, are invisible to the user, are shown together with the name(s) (s) of the theme(s) returned(s). These components, which are initially invisible, are 5 objects of the type Button, which I intend to show and fill in according to the results returned. It is not possible to click on these buttons, and in front of each one, there is a Toggle, which will define which theme is selected.
The images below the two interfaces:
Screenshot of the search panel with all components:
However, when clicking the "Search" button, the Nullreferenceexception error is generated. Below are the code snippets involved in the process:
public class pesquisar_tema : MonoBehaviour
{
// Start is called before the first frame update
private BancoDeDados bancoDeDados = new BancoDeDados();
public Text tema;
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void pesquisarTema(){
tabela table = new tabela();
table.preencherTemas(tema.text);
}
}
In function researchTema() above, which is executed when the button Research is clicked, I tried to make this script communicate with the responsible for the "table" I created, but also saw that in Unity is not made the use of instances with classes that derive from Monobehaviour, but if I take it from the script table, other errors appear.
public void preencherTemas(string tema){
Dictionary<int, string> temas = bancoDeDados.pesquisarTemas(tema);
int qtd_temas = temas.Count;
int i = 0;
//o trecho abaixo separa o dicionário contendo os temas em 2 vetores, um com o ID e o outro com o Nome
string[] tema_nome = new string[qtd_temas];
int[] cod_tema = new int[qtd_temas];
foreach (KeyValuePair<int, string> item in temas){
tema_nome[i] = item.Value;
cod_tema[i] = item.Key;
i++;
}
//
for (i = paginaTabela*5 - 5; i < (paginaTabela*5); i++){
if (i >= cod_tema.Length){
break;
}else{
if (i == (paginaTabela*5 - 5) ){
alterarAlt1(1, tema_nome[i], cod_tema[i]);
}
}
}
}
The above function is responsible for filling the table shown to the user with the return of the themes, and the problem is when calling the function amend Alt1(), which is the function below:
private void alterarAlt1(int op, string nome_tema, int cod_tema){
fundo_alternativa1 = this.transform.Find("alt_1").GetComponent<Image>();
texto_alternativa1 = this.transform.Find("alt_1/Text").GetComponent<Text>();
toogle1 = this.transform.Find("alt_1/Toggle").GetComponent<Toggle>();
toggleText1 = this.transform.Find("alt_1/Toggle/Label").GetComponent<Text>();
toggleImage1 = this.transform.Find("alt_1/Toggle/Background").GetComponent<Image>();
if (op == 0){
fundo_alternativa1.color = new Color(255, 255, 255, 0);
texto_alternativa1.text = "";
toogle1.isOn = false;
toggleImage1.color = new Color(255, 255, 255, 0);
toggleText1.text = "";
}else if (op == 1){
int codigo = cod_tema;
fundo_alternativa1.color = new Color(255, 255, 255, 1);
texto_alternativa1.text = nome_tema;
toogle1.isOn = false;
toggleImage1.color = new Color(255, 255, 255, 1);
toggleText1.text = "Selecionar";
}
}
The error occurs in the section where there are the "Getcomponent", that is, in the variables fundo_alternativa1, texto_alternativa1, toogle1, toggleText1 and toggleImage1. This same function I use inside the Start() method to make these components initially invisible to the user, sending the value 0 to the variable op inside the if, however, the error does not appear when starting the application, only when the button Research is clicked. All screen components are already instantiated when the application starts, so the panel is already "built" even if it is not initially appearing to the user. If anyone has any suggestions on how to solve this problem, I would appreciate it very much!
At no time do you assign any value to
Text tema
classpesquisar_tema
– Leandro Angelo
this variable I am assigning directly to the Inspector of Unity, why its visibility is public
– user127224
@atlas250 what is the exact line of code that is generating the Exception?
– andresantacruz
in the method
pesquisarTema()
iftema
was not instantiated, when you do ittable.preencherTemas(tema.text);
when trying to access the propertytext
you will receive aNullReferenceException
. Debug your code, method by method and line by line you will find the problem.– Leandro Angelo