Câu query trong java

đoạn code dưới e kiểm tra trong table survey_category_question xem đã tồn tại cái cái category_question trong listCategorySelected chưa… nếu chưa tồn tại thì sẽ lưu lại vào survey_category_question

nhưng nó lại báo lỗi null ở chỗ “+ category.getCategoryQuestionId() +”

if(listCategorySelected != null){
		for (CategoryQuestion category : listCategorySelected) {
			
			SurveyCategoryQuestion cts = (SurveyCategoryQuestion) getEntityManager()
					.createQuery(
							"from SurveyCategoryQuestion c where c.question.questionId="
									+ instance.getQuestionId()
									+ "andc.categoryQuestion.categoryQuestionId ="
									+ category.getCategoryQuestionId() + "")
					.getSingleResult();

			if (cts == null) {
				SurveyCategoryQuestion cq = new SurveyCategoryQuestion();
				cq.setCategoryQuestion(category);
				cq.setQuestion(instance);
				getEntityManager().persist(cq);
				getEntityManager().flush();
			}
		}
	}

NullPointetException???
Bạn chắc rằng giá trị category không rỗng (not null)?
Kiểm tra nó trước khi thao tác: if(category == null) continue;

1 Like

e kiểm tra list chứa nó not null rồi… thì e duyệt từng cái nó phải not null luôn chứ ạ

  1. listCategorySelected là kiểu dữ liệu gì?
  2. phương thức getCategoryQuestionId() được định nghĩa như thế nào?

Nếu mọi thứ có vẻ đúng mà vẫn bị thì đặt break point rồi debug

1 Like

listCategorySelected là list chứa đối tượng Cateogory và

getCategoryQuestionId :

public int getCategoryQuestionId() {
	return categoryQuestionId;
}
public void setCategoryQuestionId(intcategoryQuestionId) {
	this.categoryQuestionId = categoryQuestionId;
}

e chạy debug nó báo null ở chỗ getCategoryQuestionId… e nghĩ là e đã check null ở listCategorySelect rồi thì thằng ở trong list sao nó lại có id null

Việc list không null không đảm bảo rằng tất cả các phần tử trong list đều không phải là null.
null cũng là một giá trị, có thể có một vài slot trong list cũng không sao, nếu để chắc chắn thì check cả if (category ≠ null)

Nhưng đó chỉ là hot fix, còn để giải quyết tận gốc thì phải tìm được vấn đề trong thuật toán dẫn đến list đó có phần tử null.

2 Likes

. e có 2 model Questions và Category được liên kết bởi model CategoryQuestion
khi sửa Question em sẽ check xem người dùng có thêm hay bỏ Category cho Question đó hay không
dưới đây là đoạn em lấy ra listCategorySelect từ db

   if (instance.getQuestionId()!=null) {
		listCategorySelected.clear();
		List<SurveyCategoryQuestion> category=getEntityManager().createQuery("from SurveyCategoryQuestion c where c.question.questionId="+instance.getQuestionId()).getResultList();
		if(category!=null){
		for (SurveyCategoryQuestion surveyCategoryQuestion : category) {
			System.out.print(surveyCategoryQuestion.getQuestion().getQuestionId()+": id");
				CategoryQuestion ca = getEntityManager().find(
						CategoryQuestion.class,
						surveyCategoryQuestion.getQuestion().getQuestionId());
					listCategorySelected.add(ca);
			}
		}
	}

khi người dùng update Question và thêm mới một Category cho Question thì e add vào listCategorySelect
khi người click update thì e sẽ đi duyệt lại list và có đoạn code trên bài post

ca có thể là null?

1 Like

em có kiểm tra getQuestionId truyền vào xong vào thử db xem thì nó có mà… :sleepy:

dạ. em fix được rồi… cảm ơn 2 người ae đã giúp đỡ ạ

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?