大家来帮忙看看这两个java代码有什么错误
packagesong_dao;importjava.util.ArrayList;importsong_entitys.MCR;publicclassMCR_DAO{s...
package song_dao;
import java.util.ArrayList;
import song_entitys.MCR;
public class MCR_DAO {
static ArrayList<MCR> al=new ArrayList<MCR>();
static MCR mcr = null;
public static MCR findAll(){
mcr.setSid(12);
mcr.setSname("iyou");
mcr.setSonger("mcr");
mcr.setStype("rock&roll");
mcr.setSsize(52L);
mcr.setSform("mp3");
return mcr;
}
public static void main(String[] args){
System.out.println(new MCR_DAO().findAll());
}
}
package song_entitys;
public class MCR {
private Integer sid;
private String sname;
private String songer;
private String stype;
private long ssize;
private String sform;
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSonger() {
return songer;
}
public void setSonger(String songer) {
this.songer = songer;
}
public String getStype() {
return stype;
}
public void setStype(String stype) {
this.stype = stype;
}
public long getSsize() {
return ssize;
}
public void setSsize(long ssize) {
this.ssize = ssize;
}
public String getSform() {
return sform;
}
public void setSform(String sform) {
this.sform = sform;
}
}
错误信息:Exception in thread "main" java.lang.NullPointerException
at song_dao.MCR_DAO.findAll(MCR_DAO.java:11)
at song_dao.MCR_DAO.main(MCR_DAO.java:21)
一楼说的对,但我后来改了代码,这样为什么输不出al的元素出来啊?
public class MCR {
private Integer sid;
private String sname;
private String songer;
private String stype;
private long ssize;
private String sform;
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSonger() {
return songer;
}
public void setSonger(String songer) {
this.songer = songer;
}
public static void main(String[] arges){
MCR mcr = new MCR();
ArrayList<MCR> al=new ArrayList<MCR>();
mcr.setSid(1);
mcr.setSname("i");
al.add(mcr);
System.out.println(al.get(0).toString());
}
}
输出的结果为:song_entitys.MCR@1fc4bec
该如何写重写toString方法 展开
import java.util.ArrayList;
import song_entitys.MCR;
public class MCR_DAO {
static ArrayList<MCR> al=new ArrayList<MCR>();
static MCR mcr = null;
public static MCR findAll(){
mcr.setSid(12);
mcr.setSname("iyou");
mcr.setSonger("mcr");
mcr.setStype("rock&roll");
mcr.setSsize(52L);
mcr.setSform("mp3");
return mcr;
}
public static void main(String[] args){
System.out.println(new MCR_DAO().findAll());
}
}
package song_entitys;
public class MCR {
private Integer sid;
private String sname;
private String songer;
private String stype;
private long ssize;
private String sform;
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSonger() {
return songer;
}
public void setSonger(String songer) {
this.songer = songer;
}
public String getStype() {
return stype;
}
public void setStype(String stype) {
this.stype = stype;
}
public long getSsize() {
return ssize;
}
public void setSsize(long ssize) {
this.ssize = ssize;
}
public String getSform() {
return sform;
}
public void setSform(String sform) {
this.sform = sform;
}
}
错误信息:Exception in thread "main" java.lang.NullPointerException
at song_dao.MCR_DAO.findAll(MCR_DAO.java:11)
at song_dao.MCR_DAO.main(MCR_DAO.java:21)
一楼说的对,但我后来改了代码,这样为什么输不出al的元素出来啊?
public class MCR {
private Integer sid;
private String sname;
private String songer;
private String stype;
private long ssize;
private String sform;
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSonger() {
return songer;
}
public void setSonger(String songer) {
this.songer = songer;
}
public static void main(String[] arges){
MCR mcr = new MCR();
ArrayList<MCR> al=new ArrayList<MCR>();
mcr.setSid(1);
mcr.setSname("i");
al.add(mcr);
System.out.println(al.get(0).toString());
}
}
输出的结果为:song_entitys.MCR@1fc4bec
该如何写重写toString方法 展开
4个回答
展开全部
上一个是由于你没有实例化mcr。重写toString为public String ToString(){
return "sid:"+this.getSid+"sname:"+this.getSname+"songer:"+this.getSonger+"stype:"+this.getStype+"ssize:"+this.getSsize+"sform:"+this.getSform;
}
输出的结果为:song_entitys.MCR@1fc4bec
是对象在内存中的地址,只有重写了toString方法才可以输出里面的属性值!呵呵 希望能帮到你。
return "sid:"+this.getSid+"sname:"+this.getSname+"songer:"+this.getSonger+"stype:"+this.getStype+"ssize:"+this.getSsize+"sform:"+this.getSform;
}
输出的结果为:song_entitys.MCR@1fc4bec
是对象在内存中的地址,只有重写了toString方法才可以输出里面的属性值!呵呵 希望能帮到你。
展开全部
你要初始化一下 MCR 对象啊,你Static那里静态化了产生一个MCR的引用,但是你没有让他产生的mcr实例化呢。所以你findAll的时候根本就没有任何实例可以让你set属性值的。你可以如下修改 MCR mcr = new MCR();产生一个实例来,这样就不会报空指针的错了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
toString()方法如果不重载的话是从基类Object中继承的,会输出变量在内存中的地址,要想打印出元素的内容必须在MCR类中重写toString()方法。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你创建了msr,却不实例化它,而是将它赋值为null,之后又调用它,肯定会出这样的异常
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询