ошибка: несовместимые типы: <null>нельзя преобразовать в int, если (! _cursor.isNull (null)) с использованием android room - PullRequest
1 голос
/ 14 февраля 2020

Я использую комнату для моей android базы данных. здесь у меня есть отношения многие ко многим между классом Customer и ServiceProvider, для которого я использовал этот учебник , но после компиляции я получил эту ошибку:

/ home / omid / IntelliJIDEAProjects / Бухгалтерия / приложение / сборка / сгенерированная / ap_generated_sources / debug / out / com / omidmsl / accounting / db / CustomerDAO_Impl. java: 69: ошибка: несовместимые типы: невозможно преобразовать в int if (! _Cursor.isNull (null)) {

и он упоминается в этом коде CustomerDAO_Impl. java (компилятор создал это):

package com.omidmsl.accounting.db;

import android.database.Cursor;
import androidx.collection.ArrayMap;
import androidx.room.RoomDatabase;
import androidx.room.RoomSQLiteQuery;
import androidx.room.util.CursorUtil;
import androidx.room.util.DBUtil;
import androidx.room.util.StringUtil;
import com.omidmsl.accounting.models.Business;
import com.omidmsl.accounting.models.Customer;
import com.omidmsl.accounting.models.serviceprovider.CustomerOfServiceProvider;
import java.lang.Override;
import java.lang.String;
import java.lang.StringBuilder;
import java.lang.SuppressWarnings;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@SuppressWarnings({"unchecked", "deprecation"})
public final class CustomerDAO_Impl implements CustomerDAO {
  private final RoomDatabase __db;

  public CustomerDAO_Impl(RoomDatabase __db) {
    this.__db = __db;
  }

  @Override
  public List<Customer> getCustomers() {
    final String _sql = "SELECT * FROM Customer";
    final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 0);
    __db.assertNotSuspendingTransaction();
    final Cursor _cursor = DBUtil.query(__db, _statement, false, null);
    try {
      final int _cursorIndexOfCustomerName = CursorUtil.getColumnIndexOrThrow(_cursor, "customerName");
      final int _cursorIndexOfPhoneNumber = CursorUtil.getColumnIndexOrThrow(_cursor, "phoneNumber");
      final List<Customer> _result = new ArrayList<Customer>(_cursor.getCount());
      while(_cursor.moveToNext()) {
        final Customer _item;
        _item = new Customer();
        final String _tmpCustomerName;
        _tmpCustomerName = _cursor.getString(_cursorIndexOfCustomerName);
        _item.setCustomerName(_tmpCustomerName);
        final String _tmpPhoneNumber;
        _tmpPhoneNumber = _cursor.getString(_cursorIndexOfPhoneNumber);
        _item.setPhoneNumber(_tmpPhoneNumber);
        _result.add(_item);
      }
      return _result;
    } finally {
      _cursor.close();
      _statement.release();
    }
  }

  @Override
  public List<CustomerOfServiceProvider> getCustomersOfServiceProvider() {
    final String _sql = "SELECT * FROM Customer";
    final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 0);
    __db.assertNotSuspendingTransaction();
    __db.beginTransaction();
    try {
      final Cursor _cursor = DBUtil.query(__db, _statement, true, null);
      try {
        final int _cursorIndexOfPhoneNumber = CursorUtil.getColumnIndexOrThrow(_cursor, "phoneNumber");
        final ArrayMap<String, ArrayList<Customer>> _collectionCustomers = new ArrayMap<String, ArrayList<Customer>>();
        while (_cursor.moveToNext()) {
          if (!_cursor.isNull(null)) {
            final String _tmpKey = _cursor.getString(null);
            ArrayList<Customer> _tmpCustomersCollection = _collectionCustomers.get(_tmpKey);
            if (_tmpCustomersCollection == null) {
              _tmpCustomersCollection = new ArrayList<Customer>();
              _collectionCustomers.put(_tmpKey, _tmpCustomersCollection);
            }
          }
        }
        _cursor.moveToPosition(-1);
        __fetchRelationshipCustomerAscomOmidmslAccountingModelsCustomer(_collectionCustomers);
        final List<CustomerOfServiceProvider> _result = new ArrayList<CustomerOfServiceProvider>(_cursor.getCount());
        while(_cursor.moveToNext()) {
          final CustomerOfServiceProvider _item;
          final Business _tmpServiceProvider;
          if (! (_cursor.isNull(_cursorIndexOfPhoneNumber))) {
            _tmpServiceProvider = new Business();
            final String _tmpPhoneNumber;
            _tmpPhoneNumber = _cursor.getString(_cursorIndexOfPhoneNumber);
            _tmpServiceProvider.setPhoneNumber(_tmpPhoneNumber);
          }  else  {
            _tmpServiceProvider = null;
          }
          ArrayList<Customer> _tmpCustomersCollection_1 = null;
          if (!_cursor.isNull(null)) {
            final String _tmpKey_1 = _cursor.getString(null);
            _tmpCustomersCollection_1 = _collectionCustomers.get(_tmpKey_1);
          }
          if (_tmpCustomersCollection_1 == null) {
            _tmpCustomersCollection_1 = new ArrayList<Customer>();
          }
          _item = new CustomerOfServiceProvider();
          _item.setServiceProvider(_tmpServiceProvider);
          _item.setCustomers(_tmpCustomersCollection_1);
          _result.add(_item);
        }
        __db.setTransactionSuccessful();
        return _result;
      } finally {
        _cursor.close();
        _statement.release();
      }
    } finally {
      __db.endTransaction();
    }
  }

  @Override
  public List<Customer> getCustomer(final String cName) {
    final String _sql = "SELECT * FROM Customer WHERE customerName = ?";
    final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 1);
    int _argIndex = 1;
    if (cName == null) {
      _statement.bindNull(_argIndex);
    } else {
      _statement.bindString(_argIndex, cName);
    }
    __db.assertNotSuspendingTransaction();
    final Cursor _cursor = DBUtil.query(__db, _statement, false, null);
    try {
      final int _cursorIndexOfCustomerName = CursorUtil.getColumnIndexOrThrow(_cursor, "customerName");
      final int _cursorIndexOfPhoneNumber = CursorUtil.getColumnIndexOrThrow(_cursor, "phoneNumber");
      final List<Customer> _result = new ArrayList<Customer>(_cursor.getCount());
      while(_cursor.moveToNext()) {
        final Customer _item;
        _item = new Customer();
        final String _tmpCustomerName;
        _tmpCustomerName = _cursor.getString(_cursorIndexOfCustomerName);
        _item.setCustomerName(_tmpCustomerName);
        final String _tmpPhoneNumber;
        _tmpPhoneNumber = _cursor.getString(_cursorIndexOfPhoneNumber);
        _item.setPhoneNumber(_tmpPhoneNumber);
        _result.add(_item);
      }
      return _result;
    } finally {
      _cursor.close();
      _statement.release();
    }
  }

  private void __fetchRelationshipCustomerAscomOmidmslAccountingModelsCustomer(final ArrayMap<String, ArrayList<Customer>> _map) {
    final Set<String> __mapKeySet = _map.keySet();
    if (__mapKeySet.isEmpty()) {
      return;
    }
    // check if the size is too big, if so divide;
    if(_map.size() > RoomDatabase.MAX_BIND_PARAMETER_CNT) {
      ArrayMap<String, ArrayList<Customer>> _tmpInnerMap = new ArrayMap<String, ArrayList<Customer>>(androidx.room.RoomDatabase.MAX_BIND_PARAMETER_CNT);
      int _tmpIndex = 0;
      int _mapIndex = 0;
      final int _limit = _map.size();
      while(_mapIndex < _limit) {
        _tmpInnerMap.put(_map.keyAt(_mapIndex), _map.valueAt(_mapIndex));
        _mapIndex++;
        _tmpIndex++;
        if(_tmpIndex == RoomDatabase.MAX_BIND_PARAMETER_CNT) {
          __fetchRelationshipCustomerAscomOmidmslAccountingModelsCustomer(_tmpInnerMap);
          _tmpInnerMap = new ArrayMap<String, ArrayList<Customer>>(RoomDatabase.MAX_BIND_PARAMETER_CNT);
          _tmpIndex = 0;
        }
      }
      if(_tmpIndex > 0) {
        __fetchRelationshipCustomerAscomOmidmslAccountingModelsCustomer(_tmpInnerMap);
      }
      return;
    }
    StringBuilder _stringBuilder = StringUtil.newStringBuilder();
    _stringBuilder.append("SELECT `Customer`.`customerName` AS `customerName`,`Customer`.`phoneNumber` AS `phoneNumber`,_junction.`businessName` FROM `Service` AS _junction INNER JOIN `Customer` ON (_junction.`customerName` = `Customer`.`customerName`) WHERE _junction.`businessName` IN (");
    final int _inputSize = __mapKeySet.size();
    StringUtil.appendPlaceholders(_stringBuilder, _inputSize);
    _stringBuilder.append(")");
    final String _sql = _stringBuilder.toString();
    final int _argCount = 0 + _inputSize;
    final RoomSQLiteQuery _stmt = RoomSQLiteQuery.acquire(_sql, _argCount);
    int _argIndex = 1;
    for (String _item : __mapKeySet) {
      if (_item == null) {
        _stmt.bindNull(_argIndex);
      } else {
        _stmt.bindString(_argIndex, _item);
      }
      _argIndex ++;
    }
    final Cursor _cursor = DBUtil.query(__db, _stmt, false, null);
    try {
      final int _itemKeyIndex = 2; // _junction.businessName;
      if (_itemKeyIndex == -1) {
        return;
      }
      final int _cursorIndexOfCustomerName = CursorUtil.getColumnIndex(_cursor, "customerName");
      final int _cursorIndexOfPhoneNumber = CursorUtil.getColumnIndex(_cursor, "phoneNumber");
      while(_cursor.moveToNext()) {
        if (!_cursor.isNull(_itemKeyIndex)) {
          final String _tmpKey = _cursor.getString(_itemKeyIndex);
          ArrayList<Customer> _tmpRelation = _map.get(_tmpKey);
          if (_tmpRelation != null) {
            final Customer _item_1;
            _item_1 = new Customer();
            if (_cursorIndexOfCustomerName != -1) {
              final String _tmpCustomerName;
              _tmpCustomerName = _cursor.getString(_cursorIndexOfCustomerName);
              _item_1.setCustomerName(_tmpCustomerName);
            }
            if (_cursorIndexOfPhoneNumber != -1) {
              final String _tmpPhoneNumber;
              _tmpPhoneNumber = _cursor.getString(_cursorIndexOfPhoneNumber);
              _item_1.setPhoneNumber(_tmpPhoneNumber);
            }
            _tmpRelation.add(_item_1);
          }
        }
      }
    } finally {
      _cursor.close();
    }
  }
}

и вот остальные мои коды: Клиент. java:

package com.omidmsl.accounting.models;

import androidx.annotation.NonNull;
import androidx.room.Entity;
import androidx.room.PrimaryKey;

@Entity(tableName = "Customer")
public class Customer {

    public static final String KEY_NAME = "customer_name";
    public static final String KEY_PHONE_NUMBER = "phone_number";

    @NonNull
    @PrimaryKey
    private String customerName;
    private String phoneNumber;

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
}

CustomerDAO. java:

package com.omidmsl.accounting.db;

import androidx.room.Dao;
import androidx.room.Query;
import androidx.room.Transaction;
import com.omidmsl.accounting.models.Customer;
import com.omidmsl.accounting.models.serviceprovider.CustomerOfServiceProvider;

import java.util.List;

@Dao
public interface CustomerDAO {

    @Query("SELECT * FROM Customer")
    public List<Customer> getCustomers();

    @Transaction
    @Query("SELECT * FROM Customer")
    public List<CustomerOfServiceProvider> getCustomersOfServiceProvider();

    @Query("SELECT * FROM Customer WHERE customerName = :cName")
    public List<Customer> getCustomer(String cName);
}

Бизнес. java :

package com.omidmsl.accounting.models;

import androidx.annotation.NonNull;
import androidx.room.Entity;
import androidx.room.*;


@Entity(tableName = "Business")
public class Business {

    public static final String KEY_TYPE = "type";
    public static final String KEY_NAME = "business_name";
    public static final String KEY_PHONE_NUMBER = "phone_number";

    private int type;
    @PrimaryKey
    @NonNull
    private String businessName;
    private String phoneNumber;
    @Ignore
    private long costs;
    @Ignore
    private long buys;

    public Business(int type) {
        this.type = type;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public Business() {
        businessName = "";
    }

    public String getBusinessName() {
        return businessName;
    }

    public void setBusinessName(String businessName) {
        this.businessName = businessName;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public long getCosts() {
        return costs;
    }

    public void setCosts(long costs) {
        this.costs = costs;
    }

    public long getBuys() {
        return buys;
    }

    public void setBuys(long buys) {
        this.buys = buys;
    }
}

ServiceProviderDAO. java:

package com.omidmsl.accounting.db;

import androidx.room.Dao;
import androidx.room.Query;
import androidx.room.Transaction;
import com.omidmsl.accounting.models.serviceprovider.ServiceProviderOfCustomer;

import java.util.List;

@Dao
public interface ServiceProviderDAO {
    @Transaction
    @Query("SELECT * FROM Business")
    public List<ServiceProviderOfCustomer> getServiceProvidersOfCustomer();
}

CustomersOfServiceProvider. java:

package com.omidmsl.accounting.models.serviceprovider;

import androidx.room.*;
import com.omidmsl.accounting.models.Business;
import com.omidmsl.accounting.models.Customer;

import java.util.List;

public class CustomerOfServiceProvider {
    @Embedded
    private Business serviceProvider;
    @Relation(
            parentColumn = "businessName",
            entityColumn = "customerName",
            associateBy = @Junction(Service.class)
    )
    private List<Customer> customers;

    public Business getServiceProvider() {
        return serviceProvider;
    }

    public void setServiceProvider(Business serviceProvider) {
        this.serviceProvider = serviceProvider;
    }

    public List<Customer> getCustomers() {
        return customers;
    }

    public void setCustomers(List<Customer> customers) {
        this.customers = customers;
    }
}

пожалуйста, помогите мне

1 Ответ

0 голосов
/ 15 февраля 2020

спасибо @ ianhanniballake . задача решена!. в файле CustomerDAO я обнаружил эту проблему:

@Transaction
    @Query("SELECT * FROM Customer")
    public List<CustomerOfServiceProvider> getCustomersOfServiceProvider();

тип этого списка должен быть ServiceProvidersOfCustomer:

@Transaction
    @Query("SELECT * FROM Customer")
    public List<ServiceProviderOfCustomer> getServiceProvidersOfCustomer();
...