Reputation: 1
I need help with a java problem, when I compile it appears an error, and I already looked in other posts and could not solve if they can help me I will be very grateful.
code
import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.ArrayList;
public class Sugestao{
private int idSugestao;
private String sugestao;
private ArrayList<JTextArea> guardaSugestao;
public Sugestao(){
guardaSugestao = new ArrayList<>();
}
public Sugestao(int idSugestao){
this.idSugestao = idSugestao;
guardaSugestao = new ArrayList<>();
}
public Sugestao(int idSugestao, String sugestao){
this.idSugestao = idSugestao;
this.sugestao = sugestao;
guardaSugestao = new ArrayList<>();
}
public int getIdSugestao(){
return idSugestao;
}
public String getSugestao(){
return sugestao;
}
public ArrayList<JTextArea> guardaSugestao(){
return guardaSugestao;
}
public void setIdSugestao(int idSugestao){
this.idSugestao = idSugestao;
}
public void setSugestao(String sugestao){
this.sugestao = sugestao;
}
public void setGuardaSugestao(ArrayList<JTextArea> guardaSugestao){
this.guardaSugestao = guardaSugestao;
}
public void adicionarSugestao(JTextArea jTextArea){
guardaSugestao.add(jTextArea);
}
public ArrayList<JTextArea> carregaSugestao(Connection conn){
String sqlSelect = "select id, sugestao from tabelaTeste where id_sugestao = ?";
ArrayList<JTextArea> listaSugestao = new ArrayList<>();
try(PreparedStatement stm = conn.prepareStatement(sqlSelect);){
stm.setInt(1, getIdSugestao());
try(ResultSet rs = stm.executeQuery();){
while(rs.next()){
Sugestao s = new Sugestao();
s.setIdSugestao(rs.getInt("id_sugestao"));
s.setSugestao(rs.getString("sugestao"));
listaSugestao.add(s); // error
}
}
catch(Exception e){
e.printStackTrace();
}
catch(SQLException e1){
System.out.print(e1.getStackTrace());
}
return listaSugestao;
}
}
public String toString() {
return "Sugestao [id=" + idSugestao + ", sugestao :" + sugestao + "]";
}
}
This is the error that appears, I do not know how to solve.
error: no suitable method found for add(Sugestao)
Upvotes: 0
Views: 10733
Reputation: 4541
listaSugestao
expects elements of type JTextArea
.
But in listaSugestao.add(s);
your variable s
is of type Sugestao
.
Simply change the type of ArrayList<JTextArea>
to ArrayList<Sugestao>
and also the method definition from public ArrayList<JTextArea> carregaSugestao(Connection conn)
to public ArrayList<Sugestao> carregaSugestao(Connection conn)
and it should work.
Upvotes: 0
Reputation: 22452
In your current code, your arraylist is taking JTextArea
type as you have declared it as ArrayList<JTextArea> guardaSugestao;
So, it needs to be changed as
ArrayList<Sugestao> guardaSugestao;
so that it takes Sugestao
types into the list.
Also, ensure that your methods setGuardaSugestao()
, guardaSugestao()
, adicionarSugestao()
are changed accordingly to accept ArrayList<Sugestao>
Upvotes: 0
Reputation: 2358
The error appears because of your listaSugestao
declaration. It is an ArrayList of type JTextArea
, which only accepts JTextArea
objects and you try to add a Sugestao
object to it. You need to change the ArrayList declaration to be an ArrayList of type Sugestao
.
Change this line:
ArrayList<JTextArea> listaSugestao = new ArrayList<>();
to this:
ArrayList<Sugestao> listaSugestao = new ArrayList<>();
Upvotes: 1