当我通过spring restTemplate()从服务器获取数据时,出现kotlin.KotlinNullPointerException

时间:2019-04-18 17:13:09

标签: android spring kotlin resttemplate

我无法从服务器获取数据

我正在android kotlin上创建移动应用程序。我在应用程序中使用spring mvc框架,就像服务器一样。

我在Fragment中调用了此方法,在model.allLanguage中遇到了问题,该方法返回null

val model = UsersModel()
val lan = model.allLanguage

val placesAdapter = PlacesAdapter(lan!!, activity!!.applicationContext)
        listViewPlaces.adapter = placesAdapter
        addPlacesTypeData()
        val ll : RecyclerView = view.findViewById(R.id.listView_places_button)
        ll!!.layoutManager = LinearLayoutManager(activity!!.applicationContext, OrientationHelper.HORIZONTAL, false)
        ll!!.adapter = PlacesButtonAdapter(placesTypeList)

        ll!!.addOnItemTouchListener(
                RecyclerItemClickListener(this!!.activity!!, object : RecyclerItemClickListener.OnItemClickListener {
                    override fun onItemClick(view: View, position: Int) {
                        Toast.makeText(activity?.applicationContext, "Clicked "+position, Toast.LENGTH_SHORT).show()
                        start()
                    }
                })
        )
//UserModel
    private val BASE_URL = "http://localhost:8080/api/"
    private val restTemplate = RestTemplate()

    val allUsers: List<Users>?
        get() {
            try {
                 return restTemplate.exchange<List<Users>>(
                        BASE_URL+"getAllUsers",
                        HttpMethod.GET,
                        null,
                        object : ParameterizedTypeReference<List<Users>>() {
                        }).body
            } catch (e: Exception) {
                return null
            }
        }
//Users 
import com.fasterxml.jackson.annotation.JsonProperty

class Users {
    @JsonProperty("id")
    var id: Long? = null
    @JsonProperty("login")
    var login: String? = null
    @JsonProperty("password")
    var password: String? = null
}

0 个答案:

没有答案