You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

构造函数调用报错:Chanteur构造函数参数不匹配求助

Troubleshooting the Chanteur Constructor Mismatch Error

Hey there, let's work through this constructor error together! The error message is pointing directly to the issue, but let's break it down clearly and go over your fix options.

What the Error Is Telling You

Chanteur类的构造函数无法应用于给定类型;需要参数:int,String,String,String,Date,实际传入:int,String
原因:实际参数列表与形式参数列表长度不一致

In plain terms: The Chanteur class you're using only has a constructor that requires 5 parameters (int, String, String, String, Date), but you're trying to create an instance with just 2 parameters (the IdentC integer and NomC String from your database result set). That mismatch is why you're seeing the error.

How to Fix This

You have three solid options depending on your project's needs:

Option 1: Add a 2-Parameter Constructor to Chanteur

If your business logic only needs IdentC and NomC to create a valid Chanteur object, add a new constructor to the Chanteur class that accepts these two values. Here's an example:

public class Chanteur {
    // Your existing class fields
    private int identC;
    private String nomC;
    private String prenomC; // Example of one missing String field
    private String paysC;   // Example of the second missing String field
    private Date dateNaissance; // Example of the missing Date field

    // New constructor for your use case
    public Chanteur(int identC, String nomC) {
        this.identC = identC;
        this.nomC = nomC;
        // Initialize other fields with default values if required
        this.prenomC = "";
        this.paysC = "";
        this.dateNaissance = null;
    }

    // Keep your original 5-parameter constructor if you still need it
    public Chanteur(int identC, String nomC, String prenomC, String paysC, Date dateNaissance) {
        this.identC = identC;
        this.nomC = nomC;
        this.prenomC = prenomC;
        this.paysC = paysC;
        this.dateNaissance = dateNaissance;
    }

    // Add getters and setters as needed
}

Option 2: Fetch All Required Fields from the Database

If you must use the original 5-parameter constructor, adjust your SQL query to pull the missing fields, then pass them to the constructor.

First, update your SELECT query to include the three missing fields (replace the example names with your actual column names):

SELECT 
    album.CodeA,
    album.TitreA,
    chanteur.IdentC,
    album.SortieA,
    chanteur.NomC,
    chanteur.PrenomC, -- Missing String field 1
    chanteur.PaysC,   -- Missing String field 2
    chanteur.DateNaissance -- Missing Date field
FROM album 
INNER JOIN chanteur ON (album.IdentC=chanteur.IdentC) 
ORDER BY CodeA ASC

Then modify your Java code to pass all 5 parameters when creating the Chanteur instance:

myList.add(new Album(
    resu.getString("CodeA"),
    resu.getString("TitreA"),
    new Chanteur(
        resu.getInt("IdentC"),
        resu.getString("NomC"),
        resu.getString("PrenomC"),
        resu.getString("PaysC"),
        resu.getDate("DateNaissance")
    ),
    resu.getDate("SortieA")
));

Option 3: Use a No-Arg Constructor + Setter Methods

If Chanteur has a no-argument constructor (or you can add one), create an empty instance first, then set the fields using setter methods:

Chanteur chanteur = new Chanteur();
chanteur.setIdentC(resu.getInt("IdentC"));
chanteur.setNomC(resu.getString("NomC"));
// Set the other three fields here if needed with their respective setters

myList.add(new Album(
    resu.getString("CodeA"),
    resu.getString("TitreA"),
    chanteur,
    resu.getDate("SortieA")
));

Quick Checks to Avoid Future Issues

  • Double-check that the column names in your resu.getXXX() calls match exactly with the column names (or aliases) in your SQL query—typos here can cause unexpected SQLExceptions.
  • If you plan to scale this project, consider using an ORM tool (like Hibernate) to automate object-database mapping and avoid manual constructor parameter mismatches.

内容的提问来源于stack exchange,提问作者julliiie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:11:01